将 ivar 添加到 NSManagedObject 子类

发布于 2024-10-05 00:34:51 字数 874 浏览 4 评论 0原文

我有一个 NSManagedObject 类的子类,与 iPhone 中的 Core Data 一起使用。但是,我有一个临时“字段”(ivar),我想将其添加到该类中(但我不想将其保留在数据存储中)。尝试使用非正式和正式协议,但它们都给了我类似“静态变量”的行为。 (它的行为类似于类变量而不是实例变量)。有什么建议吗?

我的第一次尝试创建了 Test“Dummy-class”,它应该是 NSManagedObject 的子类,然后我创建了 Test-category

@interface Test (custom)
   NSString *_string  ;
  - (void)setString:(NSString *)newString;  
  - (NSString *)string;
@end

这些是通常的 setter 和 getter。这是我使用 Test 类的方式,

Test *a = [[Test alloc] init];  
Test *b = [[Test alloc] init];  
[a setString:@"Test1"];  
NSLog(@"%@", [a string]);         //This will print out Test1

[b setString:@"Test2"];          
NSLog(@"%@", [b string]);         //This will print out Test2         
NSLog(@"%@", [a string]);         //Unfortunately, this will also print out Test2

我也可以直接弄乱 NSManagedObject 子类(这是我的实体),但我不认为这是这样做的方法。

I have a subclass of NSManagedObject Class used with Core Data in iPhone. However, I have a temporary "field" (ivar) that I want to add in that Class (but I dont want to persist it in the data-store). Tried to use informal and formal protocol, but both of them give me a "static-variable" like behaviour. (It behaves like a Class Variable rather than Instance Variable). Any suggestion?

My first attempt, created Test "Dummy-class" which is supposedly a subclass of NSManagedObject, then I created Test-category

@interface Test (custom)
   NSString *_string  ;
  - (void)setString:(NSString *)newString;  
  - (NSString *)string;
@end

Those are the usual setter and getter. This is the way I use the Test class

Test *a = [[Test alloc] init];  
Test *b = [[Test alloc] init];  
[a setString:@"Test1"];  
NSLog(@"%@", [a string]);         //This will print out Test1

[b setString:@"Test2"];          
NSLog(@"%@", [b string]);         //This will print out Test2         
NSLog(@"%@", [a string]);         //Unfortunately, this will also print out Test2

I could also mess with the NSManagedObject subclass (which is my Entity) directly but I dont think that is the way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如日中天 2024-10-12 00:34:51

您不能在(正式)协议或类别中添加实例变量。

类别内的任何变量定义都被视为类别外文件级别的变量定义,因此它的行为类似于类变量。这是一种令人困惑的行为;我想编译器应该对此发出警告。

标准解决方案是在直接表示实体的子类中添加保存瞬态数据(不会持久存在数据库中)的 ivar,如下所示:

@interface MyEntity:NSManagedObject{
       NSString*stringHoldingTransientSomething;
}
      ...
@end

然后将 MyEntity 指定为 Core Data 中的类编辑。请注意,Core Data 不会自动将 ivars 保存在您的自定义 NSManagedObject 子类中;它只保存核心数据模型中指定的属性。因此,您可以在自定义子类中添加任意数量的簿记 ivars。

You can't add an instance variable in the (in)formal protocol or in the category.

Any variable definition inside the category is treated as a variable definition at the file level outside the category, so it behaves like a class variable. It's a confusing behavior; I guess the compiler should warn about it.

The standard solution is to add the ivar which holds transient data (which does not persist in the database) in the subclass representing the entity directly, as in:

@interface MyEntity:NSManagedObject{
       NSString*stringHoldingTransientSomething;
}
      ...
@end

and then specifying MyEntity as the class in the Core Data Editor. Note that Core Data does not automatically save ivars in your custom NSManagedObject subclass; it only saves the properties specified in the Core Data model. So you can add as many book-keeping ivars as you want in your custom subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文