@property|@property 的属性class

【@property|@property 的属性class】今天看欧阳大哥的MYLayout的源码,发现这么一段代码

@property(class, nonatomic, assign) BOOL isRTL;

属性中描述中有个class 。这个class 是干嘛的呢?
找了半天,找到这个连接Objective-C Class
作用 给类增加一个属性。(其实就是给类增加了一个setting 和getter方法,+方法,注意没有生成实例变量)
测试代码
#import @interface ClassProperty : NSObject @property (class, nonatomic, assign, readonly) NSInteger userCount; @property (class, nonatomic, copy) NSUUID *identifier; + (void)resetIdentifier; @end

#import "ClassProperty.h" static NSUUID *_identifier = nil; static NSInteger _userCount = 0; @implementation ClassProperty - (instancetype)init { self = [super init]; if (self) { _userCount += 1; } return self; }+ (NSInteger)userCount { return _userCount; } + (NSUUID *)identifier { if (_identifier == nil) { _identifier = [[NSUUID alloc] init]; } return _identifier; }+ (void)setIdentifier:(NSUUID *)newIdentifier { if (newIdentifier != _identifier) { _identifier = [newIdentifier copy]; } } + (void)resetIdentifier { _identifier = [[NSUUID alloc] init]; } @end

ClassProperty * obj = [ClassProperty new]; NSLog(@"%d",ClassProperty.userCount); NSLog(@"%@",ClassProperty.identifier);

测试结果
2018-08-27 14:25:14.274852+0800 OriginCodeAnalytical[96276:4482776] 1 2018-08-27 14:25:14.275168+0800 OriginCodeAnalytical[96276:4482776] 544F9332-6CFC-43F6-93D6-ECB90A5D22AD

这个属性,只是增加getting setting方法,我们需要自己添加静态变量(权当类成员变量)

    推荐阅读