@property 的屬性class

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

屬性中描述中有個(gè)class 。這個(gè)class 是干嘛的呢?

作用

增加一個(gè)屬性。(其實(shí)就是給類增加了一個(gè)setting 和getter方法,+方法,注意沒(méi)有生成實(shí)例變量)

測(cè)試代碼

#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);

測(cè)試結(jié)果

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

這個(gè)屬性,只是增加getting setting方法,我們需要自己添加靜態(tài)變量(權(quán)當(dāng)類成員變量)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容