Overriding setters with arc and dynamic properties

I need to do some additional stuff in a setter method. But I get an infinite loop when doing so:

I've got a core data object

@interface Transaction : NSManagedObject 
@property (nonatomic, retain) NSDate * date;
@end

@implementation Transaction
@dynamic date;

-(void)setDate:(NSDate *)date
{
    self.date = date;
    //additional stuff omitted
}

So, in that case I get an infinite loop. Okay so I searched on the net and modified my code in the following way and for every version I get compiler errors

Version 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
}

Version 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
}

Now, I'm asking myself how to do this?

Answers


The solution to my problem:

@interface Transaction : NSManagedObject 
@property (nonatomic, retain) NSDate * date;
@end

@implementation Transaction
@dynamic date;

-(void)setDate:(NSDate *)date
{
    [self setPrimitiveValue:date forKey:@"date"];
    //additional stuff omitted
}

Is "date" backed by a corresponding attribute in Core Data?

If so, please take a look at Custom setter methods in Core-Data

If not, and you don't need to persist "date", your code should be the following:

@interface Transaction : NSManagedObject 
@property (nonatomic, retain) NSDate * date;
@end

@implementation Transaction
@synthesize date = _date;

-(void)setDate:(NSDate *)date
{
    _date = date; 
    //additional stuff omitted
}

Need Your Help

Android 4.0.3 - Web Services updates not working properly

android cordova android-firmware

I have created an app using phonegap, which uses web services to update the html pages, images etc. This worked perfectly on my Samsung Galaxy S2 using 2.35, however as soon as I upgraded the firmw...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.