Objective-C:标量属性默认为原子?

发布于 2024-11-26 05:05:42 字数 238 浏览 2 评论 0原文

一位朋友告诉我,标量属性(BOOL、NSInteger 等)的 @property 默认值是非原子的。即,

@property BOOL followVenmo;

默认为

@property (nonatomic) BOOL followVenmo;

但是,我总是认为默认值始终是原子的,标量与否。

是哪一个?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-12-03 05:05:42

请小心这个“标量”术语。 NSString * 属性也是一个指针,与您提供的带有指向 BOOL 的指针的示例完全相同。

来自 Apple 文档:(Objective-C 编程语言)

如果您指定retaincopy并且不指定nonatomic,然后在引用计数环境中,对象属性的合成 get 访问器使用保留并自动释放返回值 - 实现将类似于以下内容:


<代码>[_内部锁]; // 使用对象级锁对象级
id 结果 = [[值保留] autorelease];
[_内部解锁];
返回结果;

您无法在非对象的对象上应用对象级锁,因此基元类型的属性中的(非)原子基本上没有任何效果。

您可以得出结论,原子仅适用于对象属性,并且文档中强化了这一点:

如果您指定nonatomic对象属性的综合访问器会直接返回值。


为了澄清是否应该指定其中之一:从技术上讲,没有非原子的属性被视为原子,但请记住它对于原始类型没有任何意义。因此,您可能希望节省一些输入并避免在其中使用nonatomic

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.

萤火眠眠 2024-12-03 05:05:42

基于我对其他几个相关问题的研究:

  • 原始类型的 Objective-c 属性
  • <一个href="https://stackoverflow.com/questions/18668082/does-atomic-actually-mean-anything-for-a-synthesized-primitive?lq=1">原子对于合成原语实际上意味着什么吗?

我将遵守@Rhubarb 的建议

根据经验,如果您不需要多线程支持(如果您使用 UIViewControllers 这样的 UI 代码,通常不需要多线程支持),那么只需将其声明为非原子的即可。

Based on my research of a couple other related questions:

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.

叶落知秋 2024-12-03 05:05:42

来自 开发者文档

非原子
指定访问器是非原子的。 默认情况下,
访问器是原子的。

原子属性确保您将获取或设置整个值。例如,从 2 个线程设置 CGRect 最终会得到其中一个,而不是两者的某种组合。

对于保留的属性,它还确保结果可以比接收者更长久。例如,您从另一个线程在调用完成之前释放的对象中获取结果,但该结果会代表您保留并自动释放,因此它仍然有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文