如何定义我班级中所有方法都可访问的STATIC数字数组???
答案 0 :(得分:11)
你在C中的表现方式相同:
static int myArray[] = { 0, 1, 2, 3, 4, 5 };
如果你想要静态NSArray
,你必须做一些技巧。 Objective-C中的对象类型不允许static
(因为你不能直接声明一个对象 - 只有指针)。在这种情况下,您需要阅读Objective-C单例。快速实现它的方法:
+ (NSArray *)myArray
{
static NSArray *theArray;
if (!theArray)
{
theArray = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
}
return theArray;
}
当然,您可以将其设置为使用您喜欢的任何类型的对象进行初始化。