问题
What's the easiest / fastest way to initialize an array of NSStrings in objective-c ?
回答1:
NSArray *array = [NSArray arrayWithObjects:@"String1",@"String2",@"String3",nil];
回答2:
C way:
NSString *s1, *s2; NSString *cArray[]={s1, s2};
The size of the cArray is 2, defined by the compiler.
More info here: How to initialize all members of an array to the same value?NSArray (Objective-C) way:
NSArray *objCArray = [NSArray arrayWithObjects:@"1", @"2", nil];
If you are using XCode4 (LLVM4.0 compiler), now you can use NSArray literals:
NSArray *array = @[ @"1", @"2" ];
More info here: What are the details of "Objective-C Literals" mentioned in the Xcode 4.4 release notes?
回答3:
NSArray *array = @[@"foo",@"bar"];
回答4:
NSString *stringArray[2] = {@"1", @"2"};
来源:https://stackoverflow.com/questions/5900123/initialize-arrays-of-nsstring-in-objective-c