@property 的属性class

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

属性中描述中有个class 。这个class 是干嘛的呢?

作用

增加一个属性。(其实就是给类增加了一个setting 和getter方法,+方法,注意没有生成实例变量)

测试代码

#import <Foundation/Foundation.h>

@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方法,我们需要自己添加静态变量(权当类成员变量)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容