Does "non-atomic" make sense in a declared readonly property (including a class property)?

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 // implementation stays the same 

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?

+6
source share
2 answers

The atomic / nonatomic modifiers do not work in your case for several reasons.

The main reason is that atomicity keywords only affect the generated code (i.e. synthesized access methods). When you declare @property in your interface and then implement it using a method (or a pair of methods) in your implementation, the compiler does not generate code, so your atomicity keyword is ignored.

There are several ways to get to this situation, and you call a couple of them:

  • First you have a class property. The compiler cannot synthesize accessors or storages for class properties, which means there is no code generation, therefore atomicity is not applied.

  • Secondly, in most common applications of readonly properties, the @property declaration is supported by a manually implemented getter method, which means that there is no code generation and, therefore, atomicity is not applied.

    (Note that you can also have instance properties declared readonly in the public interface and be synthesized due to private replication of readwrite in your implementation. In this case, not only atomicity applies, make atomicity keywords match between your public and private declarations You can also synthesize only getters and work directly with ivar support in your implementation.)

Because specifying either atomic or nonatomic for this property does nothing anyway, you can simply completely exclude atomicity keywords from your declaration. (The compiler will accept atomic , but as noted, the assumption has no effect.)

+4
source

It makes sense. A property declaration provides the user with class information. The class user can expect from - synthesized or manual - the implementation of what you say in the declaration. The user cannot even know if he is synthesized or not.

If you implement a getter (or any other accessory) yourself, you must reflect the atomicity of your implementation in the property declaration. If you have a non-atomic implementation, you should add this to the declared property.

+1
source

Source: https://habr.com/ru/post/1012120/


All Articles