0

私のアプリでは、次の方法でJSONを使用してデータをアップロードします。

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
}

NSMutableDictionary *message = [NSMutableDictionary dictionary];
for (UITextField *textField in messagetext)
{
    [message 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
}

NSMutableDictionary *all = [NSMutableDictionary dictionary];

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

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

[jsonData writeToFile: getImagePath atomically: YES];


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

唯一の問題は、最後に作成されたテーブルのテキストのみがアップロードされ、すべてがアップロードされないことです。ケース付きのタグを使用しましたが、長すぎて、オブジェクトがnilの場合、アプリがクラッシュしました。また、「メッセージ」または「メッセージ名」を個別にアップロードすることしかできず、次の結果になります。

{"title":"Untitledjjjj"}

「すべて」の全体的な目的は、次のようなものにすることでした:メッセージ1(メインキー)とキータイトルの下、対応するタイトル、およびメッセージと対応するメッセージ。次にmessage2..。

これどうやってするの??すべてアップロードすると、結果は{}になります

4

1 に答える 1

2

常に同じキーを使用しています。おそらく、との両方に代わりに配列を使用する必要がtitlesありmessageます。

最初の部分を修正します。これで十分です。

NSMutableArray *titles = [NSMutableArray array];

for (UITextField *textField in messagename)
{
    // add textfield contents to array
    [titles addObject: textField.text];
}

わかりますか?辞書では、1つのキーが持つことができる値は1つだけです。したがって、常にコンテンツをの各呼び出しに置き換えています[dicitonary setObject: ... forKey: @"title"];

于 2012-05-11T16:09:03.650 に答える