实现自定义访问器方法 [英] implementing custom accessor methods

查看:140
本文介绍了实现自定义访问器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读核心数据编程指南。它包含以下文本:

I am reading "Core Data Programming Guide". It contains this text:


但是,必须以符合KVC的方式更改属性值。
例如,以下代表一个编程错误:

You must, however, change attribute values in a KVC-compliant fashion. For example, the following typically represents a programming error:

NSMutableString * mutableString = [NSMutableString stringWithString:@Stig];
[newEmployee setFirstName:mutableString];
[mutableString setString:@Laura];


对于可变值,将值
的所有权转移到Core Data,或实现定制访问器方法以始终执行
副本。如果代表Employee实体的类
声明了firstName属性
(copy)(或实现了复制
new值的自定义setFirstName:方法),则上一个示例可能不会表示错误。在这种情况下,在调用setString:(在
第三个代码行)之后,firstName的值仍然是Stig,
不是Laura。

For mutable values, you should either transfer ownership of the value to Core Data, or implement custom accessor methods to always perform a copy. The previous example may not represent an error if the class representing the Employee entity declared the firstName property (copy) (or implemented a custom setFirstName: method that copied the new value). In this case, after the invocation of setString: (in the third code line) the value of firstName would then still be "Stig" and not "Laura".

关于文本的问题:在这种情况下,是哪种情况 - 属性被声明为复制的一个,或者不是?

Question regarding text: "In this case" is which case--the one where property is declared as "copy" or when its not?

有关复制和编程实践的问题:
从我在这里读到的:
NSString属性:复制或保留?
我理解

Question regarding copy and programming practice: From what I have read here: NSString property: copy or retain? I understand


  1. 使用复制将确保firstName是Stig,而不是Laura

  2. 这样做是明智的,因为在几乎所有情况下,你想防止突变对象的属性它的背面

我真的想知道上面引用的文本是什么,试图告诉我们在核心数据的上下文。我们必须使用复制无论使用核心数据还是不。此外,我会很高兴,如果有人可以在点2(这是明智的...)上更多的光,因为在后面会变异的对象的属性的后果是什么?

I would really like to know what is the above quoted text trying to tell us in the context of Core Data. We have to use "copy" anyway whether using Core Data or not. Also, I would be glad if someone could throw more light on point "2" (it is wise to...) above as in what will be the consequences of mutating an object's attributes behind its back?

推荐答案

您的有关文本的问题:在这种情况下,是哪种情况 - 属性被声明为副本或当它的不?

your "Question regarding text: "In this case" is which case--the one where property is declared as "copy" or when its not?" mis-matched the point that Apple document wants to explain, I believe.

苹果文档指出,如果自定义访问方法正常实现,默认实现 NOT 复制属性值。如果属性值可以是可变的并且实现NSCopying协议(例如,与NSString的情况一样),您可以复制自定义访问器中的值,以帮助保留封装(,例如,的NSMutableString作为值传递)。

As Apple document points out, if custom-accessor-method is implemented normally, the default implementation does NOT copy attribute values. If the attribute value may be mutable and implements the NSCopying protocol (as is the case with NSString, for example), you can copy the value in a custom accessor to help preserve encapsulation (for example, in the case where an instance of NSMutableString is passed as a value).

以下是复制安装程式码片段

Here is a copying setter snippet

@interface Department : NSManagedObject
{
}
@property(nonatomic, copy) NSString *name;
@end
@implementation Department
@dynamic name;
- (void)setName:(NSString *)newName
{
    [self willChangeValueForKey:@"name"];
    // NSString implements NSCopying, so copy the attribute value
    NSString *newNameCopy = [newName copy];
    [self setPrimitiveName:newNameCopy];
    [self didChangeValueForKey:@"name"];
} @end

这篇关于实现自定义访问器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆