我刚开始使用Xcode并需要一些帮助。我有这个数组设置:
myArray = [NSMutableArray arrayWithObjects: item1type1, item1type, item1type3, item1type4, item1type5, item1type6, item1type7, item1type8, item1type9, nil];
是否可以自动为序列中的元素指定数字?逻辑很简单:item1type(i),item1type(i + 1),item1type(i + 2),...
非常感谢
答案 0 :(得分: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]];