xcode 澄清分配、保留、复制、强?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9035658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:07:43  来源:igfitidea点击:

Clarification on assign, retain, copy, strong?

iphoneobjective-cxcode

提问by wayneh

I'm still new to Objective-C and having some difficulties trying to figure out the appropriate way to use assign, retain, copy, strong, etc. when setting a property.

我还是 Objective-C 的新手,并且在设置属性时试图找出使用分配、保留、复制、强等的适当方法时遇到了一些困难。

For example, I have the following types declared - how should I be setting the properties?

例如,我声明了以下类型 - 我应该如何设置属性?

@property (nonatomic, ??) NSMutableArray *myArray
@property (nonatomic, ??) NSString *myString
@property (nonatomic, ??) UIColor *myColor
@property (nonatomic, ??) int *myIn
@property (nonatomic, ??) BOOL *myBOOL

Thanks....

谢谢....

回答by David Schaefgen

To reiterate, it does depend on context. In an non-ARC situation:

重申一下,它确实取决于上下文。在非 ARC 情况下:

@property (nonatomic, copy) NSMutableArray *myArray
@property (nonatomic, copy) NSString *myString
@property (nonatomic, retain) UIColor *myColor
//Note the change to an int rather than a pointer to an int
@property (nonatomic, assign) int myInt
//Note the change to an int rather than a pointer to an int
@property (nonatomic, assign) BOOL myBOOL

The copy on myArray is to prevent modification by another "owner" of the object you set. In an ARC project, things change a bit:

myArray 上的副本是为了防止您设置的对象的另一个“所有者”进行修改。在 ARC 项目中,情况会发生一些变化:

@property (nonatomic, copy) NSMutableArray *myArray
@property (nonatomic, copy) NSString *myString
@property (nonatomic, strong) UIColor *myColor
//Note the change to an int rather than a pointer to an int
@property (nonatomic, assign) int myInt
//Note the change to an int rather than a pointer to an int
@property (nonatomic, assign) BOOL myBOOL

The change is primarily to myColor in your situation. You wouldn't use retainas you aren't managing reference counting directly. The strongkeyword is a way of asserting "ownership" of the property and similar to retain. An additional keyword is also provided, weak, that would typically be used in place of assign for object types. Apple's common example of a weakproperty is for delegates. I'd recommend going through the Transitioning to ARC Release Notesin addition to the Memory Management Guidea time or two as there is more nuance than can easily be covered in an SO post.

在您的情况下,更改主要是针对 myColor。您不会使用,retain因为您没有直接管理引用计数。该strong关键字是主张财产,类似的“所有权”的一种方式retain。还提供了一个额外的关键字weak,通常用于代替对象类型的赋值。Apple 的weak属性的常见示例是用于委托。除了内存管理指南之外,我建议您阅读过渡到 ARC 发行说明一两次,因为其中的细微差别比 SO 帖子中可以轻松涵盖的要多。

回答by wattson12

@property (nonatomic, copy) NSMutableArray *myArray
@property (nonatomic, copy) NSString *myString
@property (nonatomic, retain) UIColor *myColor
@property (nonatomic) int myIn
@property (nonatomic) BOOL myBOOL

copy mutable objects, or objects with mutable subclasses such as NSString: this stops them being modified by another owner. although i don't think its recommended by apple to use mutable objects as properties

复制可变对象,或具有可变子类的对象,例如NSString:这会阻止其他所有者修改它们。虽然我不认为苹果推荐使用可变对象作为属性

other objects are generally retained, with an exception being delegates, which are assigned to prevent ownership loops

通常会保留其他对象,但委托除外,它们被分配以防止所有权循环

primitives like intand BOOLare assigned, this is the default option for @property so doesnt need to be specified, although it doesnt hurt to add it in if you feel it helps the readability

intBOOL这样的原语被赋值,这是@property 的默认选项,所以不需要指定,尽管如果你觉得它有助于可读性,添加它也没有什么坏处