gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 20:08:36 25 4
gpt4 key购买 nike

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

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com