In my Core Data model I have a property which is a NSDate, the user can change it and it is optional. I would like to set this property's default value as today, I saw no way to do this in the Core Data Model so I subclassed NSManangedObject and added this code to the implementation file.
- (NSDate *)date {
return [NSDate date];
}
This seemed to work however the Date is always todays date, even when the user changes it, it goes back to today. And if an object was created yesterday it will change the date to today, so that all the objects date will be today.
How do I get around this, do the default date is today by the user can still change it?
Is "date" the name of the property as well? If so, then your code will always return the current date, since you would have overridden the property method.
To set a custom default value for a property, you can override the awakeFromInsert method. Inside that method, set the property value to [NSDate date]. See the docs for NSManagedObject for a description of this method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With