objective-c – 客观C中的静态数组

前端之家收集整理的这篇文章主要介绍了objective-c – 客观C中的静态数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码在C#中创建一个公共静态数组
public class A{
    public static array[] obj;
}
@H_502_4@我有另一个班B.
从B班我打电话
A.ArrayName和我得到我在类A中使用的数组.

@H_502_4@我想知道,在目标C中相当于什么

解决方法

没有特殊的语法.您只需定义一个类方法来返回静态数组. @H_502_4@例如:

@implementation A // note this is in the implementation

static NSArray *array;

+ (NSArray *)array
{
  if (!array)
    array = [[NSArray alloc] init];

  return array;
}

@end
@H_502_4@或者对于更好的代码,但稍微更好的性能(一个很好的想法在紧缩循环,但通常不值得):

@implementation A

static NSArray *array;

+ (void)initialize // this method is called *once* for every class,before it is used for the first time (not necessarily when the app is first launched)
{
  [super initialize];

  array = [[NSArray alloc] init];
}

+ (NSArray *)array
{
  return array;
}

@end
@H_502_4@要从B类访问它,你只需要:[A array]

原文链接:https://www.f2er.com/c/113661.html

猜你在找的C&C++相关文章