I’m tried to get column array like following without NSDictionary.
it works well.
But I’d like to know if objective-c literal supports this feature.
Is there a literal to do like this?
e.g array[][0]?, array[*][0], array[?][0], etc..?
NSArray *array1 = @[@"AAa", @"BBB", @"CCC"];
NSArray *array2 = @[@"AAb", @"BBB", @"CCC"];
NSArray *array3 = @[@"AAc", @"BBB", @"CCC"];
array = @[array1, array2, array3];
NSArray *result;
result = [self getColumnArray:0]; // <- get array's [*][0]
NSLog(@"result is : %@", result);
result = [self getColumnArray:2]; // <- get array's [*][2]
NSLog(@"result is : %@", result);
- (NSArray *)getColumnArray:(NSUInteger)index {
NSMutableArray *resultArray = [NSMutableArray array];
for (NSArray *item in array) {
[resultArray addObject:item[index]];
}
return resultArray;
}
Excuted result :
2012-10-29 16:00:24.550 testButton[28245:11303] result is : (
AAa,
AAb,
AAc
)
2012-10-29 16:00:24.552 testButton[28245:11303] result is : (
CCC,
CCC,
CCC
)
The following code will work:
Therefore you could do something like this to achieve your desired effect:
As far as I’m aware though, there is no syntax for:
There are several reasons for this.
%@‘s it needs (similar reasons for other uses, e.g. how would youreturn primaryArray[*][0]?)*already denotes a pointer in C and?is used in the?:ternary operator.In short, a fast-enumeration loop is the best way to do what you want, and the nested
[i][0]box syntax aids its readability slightly. I agree the wildcard syntax would be very handy in some cases (such as logging and enumeration), but I think it would be too difficult to implement succinctly across the board. Plus, since they overloaded^for blocks, they’re running out of usable operators!