如何在iphone中的可变数组中添加数字?

War*_*ior 9 iphone cocoa-touch objective-c nsnumber nsmutablearray

我是iphone开发的新手.我想要一个Nsmutable数组来保存从1到100的数字.我能做什么.我可以在for循环中实现.是否还有其他方法可以在iphone中保存数组中的数字.请帮助我出去了.谢谢.

Mar*_*ote 23

您只能在Cocoa容器中添加NSObject子类.在您的情况下,您将必须将您的整数包装在NSNumber对象中:

NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
   [array addObject:[NSNumber numberWithInt:i]];
}
Run Code Online (Sandbox Code Playgroud)

要提取值:

int firstValue = [[array objectAtIndex:0] intValue];
Run Code Online (Sandbox Code Playgroud)