【问题标题】:Non-Object Attribute in Core Data, transient propertiesCore Data 中的非对象属性,瞬态属性
【发布时间】: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


    【解决方案1】:

    在iOS中,有非常方便的NSStringFromCGRectCGRectFromNSString函数。为什么不直接使用它们并存储一个字符串?

    你的问题:

    1. 是的,您需要 2 个属性,如文档中所述。
    2. 是的,这是基于托管对象模型的。 xprimitiveX 名称是自动生成/解释的。
    3. 您需要这里的原始访问器方法才能使其成为 KVC - 原始情况并非如此。

    【讨论】:

    • Mundi:因为 NSRect 不是 Core Data 支持的类型,Apple 展示如何使用“瞬态属性”的示例是有意义的。
    • 您是在尝试解决学术问题还是编程问题?不要误会我的意思——两者都很好——但我想知道。
    • Mundi:这个话题更偏向于实际的编程问题。我只想知道如何使用“瞬态属性”,不在乎(实际上没有时间研究)为什么它以某种方式工作。但我无法到达那里,Apple 的公共文档有时太大太浅。
    • 那你应该接受我的字符串建议。做了很多次,按预期工作。另请参阅stackoverflow.com/a/2151064/427083
    • Mundi:感谢您的回答!!!不过,我还有最后一点不清楚,所以我添加了第四个问题。你能把它摆平吗?再次感谢!
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2011-11-22
    • 2013-01-09
    • 1970-01-01
    • 2011-02-10
    • 2014-03-29
    相关资源
    最近更新 更多