-4

私のアプリでは、ユーザーは無制限の UITextFields を作成できます。次に、それらすべての情報を取得し、json ファイルにアップロードします。

NSString *object;   
NSString *object2;
 NSString *object3;
NSString *object4;
NSString *object5;
NSString *size;

for (UITextField *text in array2) {


int touchedtag = text.tag;

NSUInteger tagCount = touchedtag;
switch (tagCount) {
    case 1: 

        object = [NSString stringWithFormat:@"%@ %@ %@", text.text, NSStringFromCGRect(text.frame), text.font];

        break;
    case 2: 
        object2 = [NSString stringWithFormat:@"%@ %@ %@", text.text, NSStringFromCGRect(text.frame), text.font];

        break;
    case 3: 

        object3 = [NSString stringWithFormat:@"%@ %@ %@", text.text, NSStringFromCGRect(text.frame), text.font];

        break;
    case 4: 

       object4 = [NSString stringWithFormat:@"%@ %@ %@", text.text, NSStringFromCGRect(text.frame), text.font];

        break;

    case 5: 

        object5 = [NSString stringWithFormat:@"%@ %@ %@", text.text, NSStringFromCGRect(text.frame), text.font];

        break;

    default :

        break;

}
}
    NSArray *keys = [NSArray arrayWithObjects:@"text", @"text2", @"text3", @"text4", @"text5", nil];
    NSArray *objects = [NSArray arrayWithObjects:object,object2,object3, object4, object5, nil];


NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
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/"];

}

問題は、オブジェクトの数が独立していないことです (object、object2...)。そのため、ユーザーが作成するテキスト フィールドが 5 つ未満の場合、アプリはクラッシュします。5 つを超える場合は何も起こりませんが、タグ 6 以降の情報は記録されません。作成したフィールドの数に応じてオブジェクトの数を変えるにはどうすればよいですか?

4

1 に答える 1

0

NSMutableArray の使用方法については、https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html をご覧ください。

Vector他の多くの言語でを使用するのと同じように、オブジェクトを「その場で」追加および削除できます。(Java、ActionScript、C++...)

例: (someValue は以前に宣言された変数です)

stuff = [[NSMutableArray alloc] initWithCapacity: someValue];
[stuff insertObject:object1 atIndex:0];
[stuff insertObject:object2 atIndex:1];
[stuff insertObject:object2 atIndex:2];

...

このコードは、探しているものと思われる特定の数のオブジェクトで初期化することです。NSMutableArray を使用すると、後で配列内のオブジェクトの数を調整することもできます。

于 2012-05-05T19:45:18.227 に答える