Reputation: 5747
What's the easiest / fastest way to initialize an array of NSString
s in Objective C?
Upvotes: 48
Views: 71525
Reputation: 3450
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?
Upvotes: 40
Reputation: 7691
NSArray *array = [NSArray arrayWithObjects:@"String1",@"String2",@"String3",nil];
Upvotes: 75