EDIT: This question also applies to normal declared properties (not just class properties)!
Original post:
Suppose I have a method of the public class sharedInstance
, which is currently implemented as the getter method:
@interface MyClass + (instancetype)sharedInstance; - (void)doSomething; @end @implementation MyClass + (instancetype)sharedInstance { static MyClass *shared = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ shared = [[MyClass alloc] init]; }); return shared; } @end
Access to this method in Swift 3.0 will look like this: MyClass.shared().doSomething()
So, to make it more careful, we have to change the class method to a class property (new in Xcode 8. but in fact I can not find it in Apple Docu, only in WWDC 2016 video)
@interface MyClass @property (class, nonatomic, readonly) MyClass *sharedInstance; - (void)doSomething; @end
Now in Swift code: MyClass.shared.doSomething()
So, the nonatomic/atomic
property modifier (I donβt know the exact term) even makes sense for the getter method that I implement in objc?
source share