objective-c - 如果您使用自定义 getter/setter,属性上的 "atomic"和 "nonatomic"属性是否有任何影响?

标签 objective-c

如果我在类中声明了一个属性:

@interface MyClass : NSObject
@property (atomic) NSString *myString;
@end

我实现了自定义的 getter 和 setter 方法:

@implementation MyClass

- (NSString *)myString
{
    // return something
}

- (void)setMyString
{
    // do something
}

@end

属性声明中的“atomic”属性真的有作用吗?还是仅在编译器实际创建自动 getter/setter 时才使用它?

假设“atomic”和“nonatomic”关键字对具有自定义 getter 和 setter 的属性没有任何作用,对于我们应该为这些属性使用什么属性是否有任何约定?

最佳答案

Does the atomic attribute on the property declaration actually do anything? Or is it only used if an automatic getter/setter is actually created by the compiler?

atomic 仅在编译器合成 getter/setter 时使用。也是used to check the consistency of accessors for readwrite properties :

Because the internal implementation and synchronization of atomic accessor methods is private, it’s not possible to combine a synthesized accessor with an accessor method that you implement yourself. You’ll get a compiler warning if you try, for example, to provide a custom setter for an atomic, readwrite property but leave the compiler to synthesize the getter.


is there any convention for what attribute we should use for those properties?

Apple 不要求您遵循任何约定,但您可以使用 atomicnonatomic 属性来记录您自己的代码。这将使代码的读者无需查看访问器的实现即可了解访问器的行为。

关于objective-c - 如果您使用自定义 getter/setter,属性上的 "atomic"和 "nonatomic"属性是否有任何影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36310097/

相关文章:

ios - UITableView 使用 UIRefreshControl 闪烁

ios - 以编程方式在 iPhone OS 4.0 中获取自己的电话号码

iphone - Objective-C 静态分析工具——Xcode 的插件?

objective-c - NSScrollView:使用 CMD+滚动交互进行放大,并保留响应式滚动

Objective-C 堆栈跟踪

ios - 使用谷歌登录时更改应用程序名称出现在谷歌授权页面

iphone - 第一次向下钻取后 TableView 未填充

ios - 使用sendAsynchronousRequest :queue:completionHandler: for URL request

objective-c - 如何在 Objective-C 中将 heic 转换为 jpg

iphone - 单引号内的字符串文字形式的 Objective c 枚举值