简体   繁体   English

使用 Core Data 时如何覆盖属性上的 getter?

[英]How can I override a getter on a property when using Core Data?

I want to be able to override the getter on a string property on one of my core data models and inside the getter I need to find out what the value is for that property.我希望能够在我的核心数据模型之一上的字符串属性上覆盖 getter,并且在 getter 中我需要找出该属性的值。

@interface LabTest : NSManagedObject {
}
@property (nonatomic, retain) NSString *status;
@end

@implementation LabTest

@dynamic status;

- (NSString *)status {
    NSString *tempStatus = [super valueForKey:@"status"];
    //do some checking here
    return tempStatus;
}

@end

The code above crashes the process.上面的代码使进程崩溃。 I have tried a few different things, but I think they all end up in an infinite loop with the program crashing with a code of 139.我尝试了几种不同的方法,但我认为它们都以无限循环结束,程序以 139 的代码崩溃。

What is the correct way to access a core data member in the getter like this?像这样访问 getter 中的核心数据成员的正确方法是什么?

你有没有试过[self primitiveValueForKey:@"status"]而不是[super valueForkey:@"status"]

Just in case anyone is looking for a little more info on how to override getter methods and landed on this page...以防万一有人正在寻找有关如何覆盖 getter 方法并登陆此页面的更多信息...

Inside of your property definition you can specify getter and setter methods as follows:在您的属性定义中,您可以指定 getter 和 setter 方法,如下所示:

@property (nonatomic, retain, getter = getterMethodName, setter = setterMethodName) NSString *someString;

You can specify the getter only, the setter only, or both.您可以仅指定 getter、仅指定 setter 或同时指定两者。

The managed object creator in x-code has a checkbox that is normally unchecked just for this purpose. x-code 中的 managed object creator 有一个复选框,通常只是为了这个目的而未选中。

Validation Methods http://www.nwcode.com/img/ValidationMethods.png验证方法 http://www.nwcode.com/img/ValidationMethods.png

There's a really handy Xcode Snippets menu (Xcode 12 has a + button in the top right) that has great snippets for overriding lots of common Core Data code, including KVO-compliant accessors for object-types (getters + setters).有一个非常方便的 Xcode Snippets 菜单(Xcode 12 右上角有一个+按钮),其中包含用于覆盖许多常见 Core Data 代码的很棒的片段,包括用于对象类型(getter + setter)的 KVO 兼容访问器。

动图演示

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

相关问题 在Core Data的瞬态属性的获取器中执行繁重的任务 - Performing an intensive task in the getter of a transient property in Core Data 如何使用核心数据覆盖sectionIndexTitlesForTableView? - How do you override sectionIndexTitlesForTableView using core data? 如何使用 Core Data 维护 UITableView 中的显示顺序? - How can I maintain display order in UITableView using Core Data? 如何实现/增强 Core-Data 关系 getter/setter? - How to implement/augment Core-Data relationship getter/setter? 如何打印出核心数据元素的属性? - How do I print out a property of a Core Data element? 当关系可能阻止它时,我如何确定是否可以删除核心数据 object? - How can I find out if a core-data object can be deleted when relationships could prevent it? 如何干净地覆盖属性设置器? - How do I cleanly override a property setter? 如何覆盖UIViewController中的“view”属性? - How do I override the “view” property in UIViewController? 将上下文保存在Core Data中时,如何更改实体? - How can I get the entities changed when the context is saved in Core Data? 如何使用传统的关系数据库复制核心数据模型? - How can I replicate core data model using a traditional relational database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM