EDIT: This question applies to normal declared properties as well (not only to class properties)!
Original Post:
Lets say I have the public class method sharedInstance
which is currently implemented as a 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
Accessing this method in Swift 3.0 would look like this: MyClass.shared().doSomething()
So to make it more swifty we should change the class method to a class property (new in Xcode 8. but actually I can't find it in the Apple Docu, only in WWDC 2016 video)
@interface MyClass
@property (class, nonatomic, readonly) MyClass *sharedInstance;
- (void)doSomething;
@end
// implementation stays the same
Now in Swift code: MyClass.shared.doSomething()
So does nonatomic/atomic
property modifier (don't know the exact term) even makes sense for a getter method which I implement myself in objc?
In Objective-C the implementation of an atomic property allows properties to be safely read and written from different threads. For nonatomic properties, the underlying pointer of a read value could be released when a new value is being written at the same time.
Atomic means only one thread accesses the variable (static type). Atomic is thread-safe, but it is slow. Nonatomic means multiple threads access the variable (dynamic type). Nonatomic is thread-unsafe, but it is fast.
Read-Only Computed PropertiesA computed property with a getter but no setter is known as a read-only computed property. A read-only computed property always returns a value, and can be accessed through dot syntax, but can't be set to a different value.
Strong means that as long as this property points to an object, that object will not be automatically released. In non-ARC it's a synonym for retain . Weak instead, means that the object the property points to, is free to release but only if it sets the property to nil .
The atomic
/nonatomic
modifiers have no effect in your case, for multiple reasons.
The main reason is that atomicity keywords affect only generated code (i.e. synthesized accessor methods). When you declare a @property
in your interface and then implement it with a method (or method pair) in your implementation, the compiler isn't generating code, so your atomicity keyword is ignored.
There are a few ways to get to this situation, and you're triggering a couple of them:
First, you have a class
property. The compiler can't synthesize accessors or storage for class properties — which means no code generation, so atomicity doesn't apply.
Second, in most common uses of readonly
properties, the @property
declaration is backed by a manually implemented getter method — which means there's no code generation and thus atomicity doesn't apply.
(Note you can also have instance properties declared as readonly
in a public interface and synthesized due to a private readwrite
redeclaration in your implementation. In that case, not only does atomicity apply, you have to make the atomicity keywords match between your public and private declarations. You can also synthesize just a getter and work directly with the backing ivar in your implementation.)
Because specifying either atomic
or nonatomic
for this property does nothing either way, you're free to just leave atomicity keywords out of your declaration entirely. (The compiler will assume atomic
, but as noted that assumption has no effect.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With