我是 iPhone 开发新手。我想要一个 Nsmutable 数组来保存从 1 到 100 的数字。我该怎么做?如何在 for 循环中实现?有没有其他方法可以在 iPhone 中将数字保存在数组中?
最佳答案
您只能在 Cocoa 容器中添加 NSObject 子类。在您的情况下,您必须将整数包装在 NSNumber 对象中:
NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
[array addObject:[NSNumber numberWithInt:i]];
}
要提取值:
int firstValue = [[array objectAtIndex:0] intValue];
关于iphone - 如何在 iPhone 的可变数组中添加数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2613799/