简体   繁体   English

Objective-C中用于复制属性的自定义设置器

[英]Custom setter for copy property in objective-c

Should I have to manually copy object in custom setter with copy property in objective-c? 我是否必须在Objective-C中使用具有复制属性的自定义设置器手动复制对象? For example, 例如,

I have a property: 我有一个财产:

@property (copy, nonatomic) NSString *someString;

and custom setter: 和自定义设置器:

- (void)setSomeString:(NSString *)someString
{
  //option 1
  _someString = someString;
  //option 2
  _someString = [someString copy];
  //do other stuff
}

Is it option 1 enough for custom setter or I have to use option 2 instead in order to have copied object? 选项1是否足以用于自定义设置器,或者我必须使用选项2才能复制对象?

You can do whatever you like but you should use the second option. 您可以做任何您想做的事,但应该使用第二种选择。 This is because it will be something like code documentation if other developer see it he or she will know that you copy the string just by looking at: 这是因为,如果其他开发人员看到它,它将像代码文档一样,他或她将知道您仅查看以下内容即可复制字符串:

@property (copy, nonatomic) NSString *someString;

The same if you use retain/assign it's best practice to retain/assign object in custom setter. 如果使用保留/分配,则相同,最佳做法是在自定义设置器中保留/分配对象。 It will make your code more clearer more documented and much more understandable for others developers. 这将使您的代码更清晰,文档更丰富,其他开发人员也更容易理解。

You Must use "option 2" because you are using "copy" for your property and statement : 您必须使用“选项2”,因为您正在对属性和语句使用“复制”:

_someString = [someString copy];

would create a new object in memory and assign it to your property object. 会在内存中创建一个新对象并将其分配给您的属性对象。

If you write your own custom setter, then you are 100% responsible for everything it does. 如果您编写自己的自定义设置器,那么您将对其所做的一切负责100%。 In your case, you are 100% responsible for the bug that you introduce with option 1 of your code: If I set someObject.someString to a mutable string and change the string later, then the string in your object will change which is most definitely not what you expect from a copy property. 在您的情况下,您对代码选项1中引入的错误负100%的责任:如果我将someObject.someString设置为可变字符串,然后再更改该字符串,则对象中的字符串将改变,这绝对是肯定的不是您对复制属性的期望。

In addition, if you have both a custom setter and getter (or a custom getter for a read-only property) so there is no compiler generated getter or setter, the compiler doesn't create the instance variable for you; 另外,如果您同时具有一个自定义的setter和getter(或一个只读属性的自定义的getter),则没有编译器生成的getter或setter,则编译器不会为您创建实例变量。 you'll have to do that yourself. 您必须自己做。

You should also notice that "copy" of immutable objects doesn't actually copy them but just retains them, so usually the "copy" operation is really cheap, and when I pass a mutable object, "copy" was most likely what you wanted anyway. 您还应该注意,对不可变对象的“复制”实际上并不复制它们,而是保留它们,因此通常,“复制”操作确实很便宜,并且当我传递可变对象时,“复制”很可能就是您想要的无论如何。

Go with option 2 if you want to make own setter but don't forget to add the condition before the statement like... 如果您想创建自己的二传手,但不要忘记在语句之前添加条件,请选择选项2。

if(_someString) {
    [_someString release]; 
}

Note:- In case of ARC use statement " _someString = nil " instead of " [_someString release] "; 注意:-如果是ARC,请使用语句“ _someString = nil ”代替“ [_someString release] ”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM