[英]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.