如何完成这项工作?简单数组循环问题Objective-C
numbers = [[NSMutableArray alloc] initWithObjects: ({int x = 0; while (x <= 60) { return x; x++; } })];
谢谢:)
如何完成这项工作?简单数组循环问题Objective-C
numbers = [[NSMutableArray alloc] initWithObjects: ({int x = 0; while (x <= 60) { return x; x++; } })];
谢谢:)
NSMutableArray * array = [[NSMutableArray alloc] init];
for (int i = 0; i <= 60; ++i) {
[array addObject:[NSNumber numberWithInt:i]];
}
谢谢:) - – Emil
int myStrangeNumberOfItems = 61;
NSMutableArray * numbers = [[NSMutableArray alloc] initWithCapacity: myStrangeNumberOfItems];
for (int i = 0; i < myStrangeNumberOfItems; i++) {
[numbers addObject:[NSNumber numberWithInt:i]];
}
首先,一个NSArray只能容纳对象,而不是原语。您可以像这样在for循环中添加对象。
NSMutableAray * numbers = [[NSMutableArray alloc] init];
for (int x = 0; x <= 60; x++)
[numbers addObject:[NSNumber numberForInt:x]];
哈!你是一个Javascript/Ruby/Scheme/ML程序员吗? –
是的:) 嘿嘿,习惯于其他方式做事情,Objective-C对我来说是不寻常的。 :P – Emil