也许这很明显,但我不知道如何在Objective-C中声明类属性。

我需要缓存每个类的字典,不知道如何把它放在类。


当前回答

从Xcode 8开始,Objective-C现在支持类属性:

@interface MyClass : NSObject
@property (class, nonatomic, assign, readonly) NSUUID* identifier;
@end

因为类属性永远不会合成,所以你需要编写自己的实现。

@implementation MyClass
static NSUUID*_identifier = nil;

+ (NSUUID *)identifier {
  if (_identifier == nil) {
    _identifier = [[NSUUID alloc] init];
  }
  return _identifier;
}
@end

在类名上使用普通的点语法访问类属性:

MyClass.identifier;

其他回答

这里有一个线程安全的方法:

// Foo.h
@interface Foo {
}

+(NSDictionary*) dictionary;

// Foo.m
+(NSDictionary*) dictionary
{
  static NSDictionary* fooDict = nil;

  static dispatch_once_t oncePredicate;

  dispatch_once(&oncePredicate, ^{
        // create dict
    });

  return fooDict;
}

这些编辑确保只创建一次fooDict。

来自Apple文档:“dispatch_once—在应用程序的生命周期中执行一次且仅执行一次块对象。”

属性只在对象中有值,在类中没有。

如果需要为类的所有对象存储一些内容,则必须使用全局变量。您可以通过在实现文件中声明它为静态来隐藏它。

您还可以考虑在对象之间使用特定的关系:将master角色属性为类的特定对象,并将其他对象链接到此master。master将把字典作为一个简单属性保存。我想到了可可应用程序中用于视图层次结构的树。

另一种选择是创建一个专用类的对象,该对象由您的“类”字典和与该字典相关的所有对象的集合组成。这类似于NSAutoreleasePool in Cocoa。

我使用这个解决方案:

@interface Model
+ (int) value;
+ (void) setValue:(int)val;
@end

@implementation Model
static int value;
+ (int) value
{ @synchronized(self) { return value; } }
+ (void) setValue:(int)val
{ @synchronized(self) { value = val; } }
@end

我发现它非常有用,可以替代Singleton模式。

要使用它,只需使用点符号访问您的数据:

Model.value = 1;
NSLog(@"%d = value", Model.value);

正如在WWDC 2016/XCode 8上看到的那样(LLVM会话@5:05的新内容)。类属性可以如下声明

@interface MyType : NSObject
@property (class) NSString *someString;
@end

NSLog(@"format string %@", MyType.someString);

注意,从不合成类属性

@implementation
static NSString * _someString;
+ (NSString *)someString { return _someString; }
+ (void)setSomeString:(NSString *)newString { _someString = newString; }
@end

从Xcode 8开始,Objective-C现在支持类属性:

@interface MyClass : NSObject
@property (class, nonatomic, assign, readonly) NSUUID* identifier;
@end

因为类属性永远不会合成,所以你需要编写自己的实现。

@implementation MyClass
static NSUUID*_identifier = nil;

+ (NSUUID *)identifier {
  if (_identifier == nil) {
    _identifier = [[NSUUID alloc] init];
  }
  return _identifier;
}
@end

在类名上使用普通的点语法访问类属性:

MyClass.identifier;