Objective c 初始化数字数组

Objective c 初始化数字数组,objective-c,ios,nsarray,nsnumber,Objective C,Ios,Nsarray,Nsnumber,我有一个不可变的数组,它只需要有数字。我必须如何初始化它 我现在有下面这样的方法,但我想一定有更好的方法。我们能像C++那样做吗?类似这样的intlist[5]={1,2,3,4,5}这会对应用程序产生任何影响吗 myArray = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil]; 另外,如果我需要

我有一个不可变的数组,它只需要有数字。我必须如何初始化它

我现在有下面这样的方法,但我想一定有更好的方法。我们能像C++那样做吗?类似这样的
intlist[5]={1,2,3,4,5}这会对应用程序产生任何影响吗

myArray = [[NSArray alloc] initWithObjects: [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];
另外,如果我需要一个只包含数字的数组,它会是什么样子?我是obj-c的新手,在网上我看到了相互矛盾的答案。

还没有,但很快:

更新:新语法为:

@[ @(20), @(10) ]

@[]
创建一个数组,
@(数字)
生成一个可以放入数组中的NSNumber。

如果它是不可变的并且只包含数字,则直接使用C数组。在Objective-C中使用C数组没有什么错,在您的情况下,
NSArray
是不必要的开销。

您可以向NSArray添加一个类别:

@implementation NSArray ( ArrayWithInts )

+(NSArray*)arrayWithInts:(const int[])ints count:(size_t)count
{
    assert( count > 0 && count < 100 ) ;    // just in case

    NSNumber * numbers[ count ] ;
    for( int index=0; index < count; ++index )
    {
        numbers[ index ] = [ NSNumber numberWithInt:ints[ index ]] ;
    }

    return [ NSArray arrayWithObjects:numbers count:count ] ;
}
@end
这样使用:

#define countof(x) (sizeof(x)/sizeof(x[0]))
const int numbers[] = { 20, 10, 5, 2, 1, 0 } ;  
NSArray * array = [ NSArray arrayWithInts:numbers count:countof(numbers) ] ) ;

或者你可以使用上面@CRD的建议…

谢谢,这就是我想要的。二维数组呢?我们也可以用C语法吗?