美文网首页Tech
class invoke @property

class invoke @property

作者: tom__zhu | 来源:发表于2017-12-15 16:39 被阅读0次

NSURLSession *session = [NSURLSession sharedSession];

初看这个方法是类方法调用,但sharedSession是property。
仔细查看这个属性的modifiers

@interface NSURLSession : NSObject
/*
* The shared session uses the currently set global NSURLCache,
* NSHTTPCookieStorage and NSURLCredentialStorage objects.
*/
@property (class, readonly, strong) NSURLSession *sharedSession;

懂了,里面有一个class关键字。

写个例子测试下

@interface SomeClass: NSObject
@property (class, nonatomic, strong) NSString *haha;
@end

@implementation SomeClass
+ (NSString *)haha {
return @"haha";
}
+ (void)setHaha:(NSString *)haha {
// 写不下去了
}
@end

查文档:http://clang.llvm.org/docs/AutomaticReferenceCounting.html#property-declarations
没找到解释……

最终得知是LLVM在Xcode8发布时的新特性,静态对象保存属性而已。
参考这片文章:https://useyourloaf.com/blog/objective-c-class-properties/

相关文章

网友评论

    本文标题:class invoke @property

    本文链接:https://www.haomeiwen.com/subject/ydydwxtx.html