开发者

Objective C - How can i define a STATIC array of numbers accessible to all methods in my class?

How can i define a STATIC array of numbers accessible to all methods in my cl开发者_StackOverflow社区ass???


The same way you'd do it in C:

static int myArray[] = { 0, 1, 2, 3, 4, 5 };

If you want a static NSArray, you'll have to do some tricks. static isn't allowed for object types in Objective-C (since you can't declare an object directly - only pointers). In that case, you need to read up on Objective-C singletons. A quick way to implement it:

+ (NSArray *)myArray
{
  static NSArray *theArray;
  if (!theArray)
  {
    theArray = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:0], nil];
  }
  return theArray;
}

You can, of course, set it up to initialize with whatever kind of objects you'd like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜