【发布时间】:2014-07-01 10:12:21
【问题描述】:
读完这一段感到迷茫:A Non-Object Attribute
根据上面链接中也包含的基本方法,在处理“瞬态属性”时,我的自定义代码中应该有2个属性:
第一个属性,用于实际需要(不支持)的自定义类型 => 瞬态属性
第二个属性,用于阴影表示(具体支持)类型 => 持久属性
......
我的阅读非常愉快,直到到达“A Non-Object Attribute”部分,这让我很困惑,如下所示:
...当您实现实体的自定义类时,通常会为该属性添加一个实例变量。 ... 《好的,我可以按照这个...做一个iVar没什么大不了的》
如果使用实例变量来保存属性,还必须实现原始的 get 和 set 访问器 《好吧,我知道怎么做primitive-accessor了。为什么需要它们?因为MO内部的内部优化存储可以有效地使用,我猜。》
@interface MyManagedObject : NSManagedObject
{
 NSRect myBounds; // I assume this suppose to be the **transient attribute**
}
@property (nonatomic, assign) NSRect bounds; // I assume this is the **persistent attribute**
@property (nonatomic, assign) NSRect primitiveBounds; // because complier forces me to implement below primitive-accessors ?
@end
- (NSRect)primitiveBounds
{
return myBounds; // accessing iVAR storage for **transient attribute**? I hope so
}
- (void)setPrimitiveBounds:(NSRect)aRect
myBounds = aRect; // accessing iVAR storage for **transient attribute**? I hope so
}
从下面,我……太多了????????????未解决
- (NSRect)bounds
{
[self willAccessValueForKey:@"bounds"]; //KVO notice of access **persistent attribute**, I guess
NSRect aRect = bounds; //will this invoke primitive-Getter ???
[self didAccessValueForKey:@"bounds"];
if (aRect.size.width == 0) //bounds has not yet been unarchived, Apple explained
 {
NSString *boundsAsString = [self boundsAsString]; // unarchiving pseudo method, I guess
if (boundsAsString != nil) //if that value is not nil, transform it into the appropriate type and cache it...Apple explained.
{
bounds = NSRectFromString(boundsAsString); //will this invoke primitive-Setter???
}
}
return bounds;
}
我把我的最终问题列表放在这里:
1,我是否STILL需要2个属性来处理NON-Object-Attribute,transient属性和persistent属性? p>
2,如何用“@property bounds”表示/连接iVar“myBounds”?这个“@property bounds”是 MOM 中的建模属性吗?
3,这里实现primitive-accessor的目的是什么?为了强制我写 KVO (will...did...) 方法对?用于在 iVar "myBounds" 和 "@property bounds" 之间传输值(输入和输出)?
4、在这行代码中
bounds = NSRectFromString(boundsAsString); //will this invoke primitive-Setter???
是调用原始设置器还是调用公共/标准设置器?为什么?
【问题讨论】:
标签: ios objective-c cocoa core-data