简单的数组循环问题Objective-C

时间:2010-02-09 17:34:41

标签: iphone objective-c xcode arrays loops

你怎么能做到这一点?

numbers = [[NSMutableArray alloc] initWithObjects: ({int x = 0; while (x <= 60 ) { return x; x++; } })];

谢谢:)

3 个答案:

答案 0 :(得分:9)

NSMutableArray * array = [[NSMutableArray alloc] init];

for (int i = 0; i <= 60; ++i) {
  [array addObject:[NSNumber numberWithInt:i]];
}

答案 1 :(得分:2)

int myStrangeNumberOfItems = 61;

NSMutableArray * numbers = [[NSMutableArray alloc] initWithCapacity: myStrangeNumberOfItems];
for (int i = 0; i < myStrangeNumberOfItems; i++) {
    [numbers addObject:[NSNumber numberWithInt:i]];
}

答案 2 :(得分:2)

首先,NSArray只能保存对象,而不是基元。您可以像这样在for循环中添加对象。

NSMutableAray * numbers = [[NSMutableArray alloc] init];
for (int x = 0; x <= 60; x++)
    [numbers addObject:[NSNumber numberForInt:x]];