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

 


作者:充满活力的早晨
链接:https://www.jianshu.com/p/add91bae977e
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
posted @ 2019-07-01 16:56  jxst441192  阅读(1105)  评论(0编辑  收藏  举报