开发者

Simple array loop question Objective-C

开发者 https://www.devze.com 2022-12-20 03:21 出处:网络
How can you make this work? 开发者_JAVA百科 numbers = [[NSMutableArray alloc] initWithObjects: ({int x = 0; while (x <= 60 ) { return x; x++; } })];

How can you make this work?

开发者_JAVA百科
numbers = [[NSMutableArray alloc] initWithObjects: ({int x = 0; while (x <= 60 ) { return x; x++; } })];

Thanks :)


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

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


int myStrangeNumberOfItems = 61;

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


First, an NSArray can only hold objects, not primitives. You can add the objects within a for loop like so.

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

精彩评论

暂无评论...
验证码 换一张
取 消