2012-05-09 6 views
0

私のアプリでは、ユーザーはUITextFieldsを作成できます。各フィールドにタグがタグの場合に対応するように、添加される:1、2、3、4、...私はNSDictionaryの内のすべてを追加し、JSON表現:NSDictionaryのJSON表現

-(IBAction)buttonDropBoxuploadPressed:(id)sender{ 

//screenshot 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy_MM_dd"]; 


NSString *filename = [NSString stringWithFormat:@"By: %@ ", 
         [formatter stringFromDate:[NSDate date]]]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString* path = [documentsDirectory stringByAppendingPathComponent: 
        [NSString stringWithFormat:@"%@", filename] ]; 

//[data writeToFile:path atomically:YES]; 


//NSString *destDir = @"/sandbox/"; 
// [[self restClient] uploadFile:filename toPath:destDir 
//     withParentRev:nil fromPath:path]; 

// [[self restClient] loadMetadata:@"/sandbox/"]; 

//JSON 


NSString *object; 
NSString *object2; 
NSString *object3; 
NSString *object4; 
NSString *object5; 
NSString *object6; 
NSString *object7; 
NSString *object8; 
NSString *object9; 
NSString *object10; 
NSString *object11; 
NSString *object12; 
NSString *object13; 
NSString *object14; 
NSString *object15; 


for (UITextField *text in messagename) { 


    int touchedtag = text.tag; 

    NSUInteger tagCount = touchedtag; 
    switch (tagCount) { 

     case 1: 

      object = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 2: 
      object2 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 3: 

      object3 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 4: 

      object4 = [NSString stringWithFormat:@" %@", text.text]; 

      break; 

     case 5: 

      object5 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 


     case 6: 

      object6 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 

     case 7: 

      object7 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 8: 

      object8 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 9: 

      object9 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 10: 

      object10 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 11: 

      object11 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 12: 

      object12 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 13: 

      object13 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 14: 

      object14 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 
     case 15: 

      object15 = [NSString stringWithFormat:@"%@", text.text]; 

      break; 


     default : 

      break; 



    } 
} 
//arrays 

NSString * objects[] = { object, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13, object14, object15}; 

NSMutableArray *textnameobject = [[NSMutableArray alloc] initWithCapacity:b]; 

textnameobject = [NSMutableArray arrayWithObjects:objects count:b]; 



NSMutableArray *textnamekeys = [[NSMutableArray alloc] initWithCapacity:b]; 
NSString * textnumber[] = {@"title", @"title", @"title",@"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title"}; 

textnamekeys = [NSMutableArray arrayWithObjects:textnumber count:b]; 


//arrays 

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject: textnameobject forKey:textnamekeys]; 

/* 

NSArray *objects2 = [NSArray arrayWithObjects:jsonDictionary, nil]; 
NSArray *keys2 = [NSArray arrayWithObjects:allkeys, nil]; 

NSDictionary *mainDict = [NSDictionary dictionaryWithObjects:objects2 forKeys:keys2]; 

*/ 

NSString* jsonString = [jsonDictionary JSONRepresentation]; 
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; 

[jsonData writeToFile:path atomically:YES]; 

NSString *destDir = @"/sandbox/"; 
[[self restClient] uploadFile:filename toPath:destDir 
       withParentRev:nil fromPath:path]; 

[[self restClient] loadMetadata:@"/sandbox/"]; 

//JSON  

} 

Iをボタンを押すと、次のエラーが表示されます。

JSONRepresentation failed。エラートレースは、( "エラードメイン= org.brautaset.JSON.ErrorDomainコード= 1 \" JSONオブジェクトキーは文字列\ "のUserInfo = 0x2e8370 {NSLocalizedDescription = JSONオブジェクトのキーが文字列でなければならない}" でなければならない)

結果としてドロップボックスエラーが発生します。これは私の以前のアプリで動作し、コードはまったく同じです。 jsonライブラリが正しく追加されます。理解できません!!助けてください!

+0

私はあなたの質問に対する答えはわかりませんが、私はそのひどい 'switch'文を書き直せますか? –

+0

また、違法な呼び出しを行う直前から、あなたが 'NSDictionary'の内容を私たちに与えてくればもっと簡単になるかもしれません。 –

+0

はい、switch文が長すぎます – Alessandro

答えて

1

コードを書き直しました。

- (IBAction)buttonDropBoxUploadPressed: (id)sender 
{ 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat: @"yyyy_MM_dd"]; 

    NSString *filename = [NSString stringWithFormat: @"By: %@ ", [formatter stringFromDate: [NSDate date]]]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex: 0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent: filename]; 

    NSMutableDictionary *titles = [NSMutableDictionary dictionary]; 
    for (UITextField *textField in messagename) 
    { 
     [titles setObject: textField.text forKey: @"title"]; 
     // as you can see, here you're replacing the value @ at key "title" with a new object on every pass 
    } 

    NSString *jsonString = [titles JSONRepresentation]; 
    NSData *jsonData = [jsonString dataUsingEncoding: NSUTF8StringEncoding]; 

    [jsonData writeToFile: path atomically: YES]; 

    NSString *destDir = @"/sandbox/"; 
    [[self restClient] uploadFile: filename toPath: destDir withParentRev: nil fromPath: path]; 
    [[self restClient] loadMetadata: @"/sandbox/"]; 
} 

しかし、私のコメントに関しては、実際にテキストフィールドのテキストを使用可能なものにシリアル化しているわけではありません。このの終わりには、最高の状態では、あなたはこのようなものがあるでしょう:

{ 
    "title": "My Text Field Value" 
} 

私はまた、あなたのテキストフィールドテキストの1つ以上がある、nilであること、比較的確信しているもののJSONの問題を引き起こします。

+0

ですが、この場合、各ラベルのテキストが表示されます。あなたのコードで最後に作成したコードのコードを取得します – Alessandro

+0

はい、値を取得します。そして、それらをすべてあなたの辞書の '' title "'キーにセットして、上書きするか、衝突を起こします。私のコードはあなたのものと機能的に同じで、あなたはその問題を見ることができます。 –

関連する問題