0

我有兴趣在视图控制器中将属性设置为 NSManagedObject 的子类,比如 Person,它将指定 person 的实例,以便我能够使用控制器中的方法进行更新。我可以这样做吗?

// Viewcontroller.h

@implementation

@property (nonatomic, retain) Person* currentPerson;

@end

// ViewController.m

@implementation

@dynamic currentPerson;

-(void) doSomethingToCurrentPerson {
    currentPerson.SomeAtrribute=somevalue;
}  

@end

看起来如果这不是一个有效的方法,也可以设置一个唯一标识符,然后将 CurrentPersonUniqueID 存储为属性并使用 KVC。有没有办法让我发布的工作内容符合我的要求,或者我最好使用更接近 KVC 方法的东西,或者完全不同的东西?

4

1 回答 1

0

@dynamic在您替换为之前,此代码将不起作用@synthesize@dynamic告诉编译器-setCurrentPerson:-currentPerson在其他地方实现,但事实并非如此。

因此@synthesize currentPerson将自动创建currentPerson's getter/setter。这PersonNSManagedObject.

此外,您不能currentPerson直接使用此名称访问,您必须使用它的 getter:

self.currentPerson.attribute = something;
// or
[self currentPerson].attribute = something;

正确代码:

// Viewcontroller.h
@implementation    
@property (nonatomic, retain) Person* currentPerson;
@end

// ViewController.m
@implementation
@synthesize currentPerson;

-(void) doSomethingToCurrentPerson {
    self.currentPerson.SomeAtrribute = somevalue;
}

@end
于 2012-08-29T18:48:07.017 回答