Objective-C: Defaults to atomic for scalar properties?

蓝咒 提交于 2019-12-10 03:57:34

问题


A friend told me that the @property default for scalar properties (BOOL, NSInteger, etc.) is nonatomic. I.e.,

@property BOOL followVenmo;

defaults to

@property (nonatomic) BOOL followVenmo;

But, I was always under the impression that the default is always atomic, scalar or not.

Which is it?


回答1:


Be careful with this "scalar" terminology. An NSString * property is also a pointer, exactly like the example you provided with a pointer to BOOL.

From Apple docs: (The Objective-C Programming Language)

If you specify retain or copy and do not specify nonatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:

[_internal lock]; // lock using anobject-levellock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

You can't apply an object-level lock in something that's not an object, so (non)atomic in properties of primitive types has basically no effect.

You can conclude that atomic only applies to object properties, and this is reinforced in the docs:

If you specify nonatomic, a synthesized accessor for an object property simply returns the value directly.

To clarify whether you should specify one or the other: technically, properties without a nonatomic are considered atomic, but remember that it has no meaning for primitive types. Thus, you may want to save some typing and avoid nonatomic in these.




回答2:


Based on my research of a couple other related questions:

  • Objective-c properties for primitive types
  • Does atomic actually mean anything for a synthesized primitive?

I shall abide by @Rhubarb's recommendation:

As a rule of thumb, if you don't need multithreaded support - which you generally don't if you're working in UI code like UIViewControllers, then just declare it all nonatomic.




回答3:


From the Developer Documentation

nonatomic Specifies that accessors are nonatomic. By default, accessors are atomic.

Atomic properties ensures that you will get or set a whole value. For example, setting a CGRect from 2 threads will end up with one or the other, not some combination of the two.

For retained properties, it also ensures that the result can outlive the receiver. For example, you get a result from an object that is released by another thread before the call finishes, but the result is retained and autoreleased on your behalf so it is still valid.



来源:https://stackoverflow.com/questions/6837334/objective-c-defaults-to-atomic-for-scalar-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!