我需要在setter方法中进行一些额外的操作。但是当我这样做时,会出现无限循环:
我有一个核心数据对象
@interface Transaction : NSManagedObject
@property (nonatomic, retain) NSDate * date;
@end
@implementation Transaction
@dynamic date;
-(void)setDate:(NSDate *)date
{
self.date = date;
//additional stuff omitted
}
那么,在这种情况下,我会得到一个无限循环。好的,所以我在网上搜索并按照以下方式修改了我的代码,但每个版本都出现编译器错误。
版本1:
@interface Transaction : NSManagedObject
@property (nonatomic, retain) NSDate * date;
@end
@implementation Transaction
@dynamic date;
-(void)setDate:(NSDate *)date
{
self->date = date; //Error: Property 'date' found on object 'Transaction *'; did you mean to access it with the "." operator?
//additional stuff omitted
}
版本2:
@interface Transaction : NSManagedObject
@property (nonatomic, retain) NSDate * date;
@end
@implementation Transaction
@dynamic date = _date; //Error: Expected ';' after @dynamic
-(void)setDate:(NSDate *)date
{
_date = date;
//additional stuff omitted
}
现在,我正在思考如何做到这一点?