-4

i'm just starting out with Xcode and need some help. I have this array set up:

myArray = [NSMutableArray arrayWithObjects: item1type1, item1type, item1type3, item1type4, item1type5, item1type6, item1type7, item1type8, item1type9, nil];

Is it possible to auto-assign numbers to elements in a sequence? The logic is simple: item1type(i), item1type(i+1), item1type(i+2),...

Many thanks

4

1 回答 1

0

这段代码中有太多的幻数,但是你去吧:

// Build an Array of keys @[@"item_1_type_1", @"item_1_type_2", ..., @"item_1_type_9"] programmatically

const int numberOfElements = 9;
NSMutableArray *keys = [NSMutableArray arrayWithCapacity:numberOfElements];
for (int i = 1; i <= numberOfElements; i++) {
    [keys addObject:[NSString stringWithFormat:@"item_1_type_%d", i]];
}

// Extract chosen keys from JSON Dictionary into an Array, matching the
// order of the keys above.

NSDictionary *subDict = myJSONData[@"item1"];
NSArray *myArray = [subDict objectsForKeys:keys notFoundMarker:[NSNull null]];
于 2013-07-25T18:13:10.090 回答