I have an object with a property with a Date type defined in my xcdatamodeld object. Why has it generated the NSManagedObject class with a NSTimeInterval? And how do I set a NSDate on it and then get an NSDate back off it?
An object space to manipulate and track changes to managed objects.
To save an object with Core Data, you can simply create a new instance of the NSManagedObject subclass and save the managed context. In the code above, we've created a new Person instance and saved it locally using Core Data.
Overview. A managed object has an associated entity description ( NSEntityDescription ) that provides metadata about the object, including the name of the entity that the object represents and the names of its attributes and relationships.
I get this if I check the "Use scalar properties for primitive data types" checkbox when I'm generating my files.
This is because NSTimeInterval is a double in disguise, whereas NSDate is a class that inherits from NSObject.
If you want to have mixed entities that uses both scalar and non-scalar property representations such as int32_t
for integer or boolean properties but NSDate*
for date properties, you need to manually edit the generated managed object .h
file.
Therefore I usually start with creating managed object classes by checking the option Use scalar properties for primitive data types (as I have more integer and boolean properties than dates). Then I edit the created header file as follows:
MYEntity.h
@interface MYEntity : NSManagedObject
@property (nonatomic) int32_t index;
@property (nonatomic) NSTimeInterval date;
@end
then becomes:
@interface MYEntity : NSManagedObject
@property (nonatomic) int32_t index;
@property (nonatomic, retain) NSDate * date;
@end
If you are of the kind who doesn't like to manually edit this file, you can also choose to add a convenience property using a category that allows for accessing the scalar property as if it would be an object.
MYEntity+Convenience.h:
@interface MYEntity (Convenience)
@property (nonatomic, readwrite) NSDate *theDate
@end
MYEntity+Convenience.h:
@implementation MYEntity (Convenience)
- (NSDate *)theDate {
return [NSDate dateWithTimeIntervalSinceReferenceDate:self.date];
}
- (void)setTheDate:(NSDate *)theDate {
self.date = [theDate timeIntervalSinceReferenceDate]
}
@end
To make the code more readable in this example I would name the original property dateTimeInterval
in the data model designer and the convenience property with its desired name: date
.
The second approach obviously also works vice versa. You can export your entities with non-scalar properties and then provide a convenience scalar property whose getter and setter accesses the original non-scalar property.
Edit: apparently the dateWithTimeIntervalSince1970
is for NSDateFormatter
only. NSDate
uses a different reference than NSDateFormatter
, which as explained in the comments is in 2001.
Sorry for my ignorance.
If you have "Use scalar properties for primitive data types" selected when you generated the NSManagedObject
subclasses for your entities, it will use scalar properties like int and float instead of NSNumber
. This includes NSDate; It will turn NSDate
into an NSTimeInterval
which is just a double. This NSTimeInterval
is a time interval since the 1970 standard epoch used by Apple (if it's before 1970 it probably is negative).
It's pretty simple to convert the NSTimeInterval
back into an NSDate
if you need it to, but if you don't have a huge database, then you might not want to bother selecting that checkbox.
To convert this time interval into an NSDate
just use [NSDate dateWithTimeIntervalSince1970: timeInterval];
where timeInterval
is what you get from the database. This can be put in your NSManagedObject
subclass so that when you get that property, you get an NSDate
anyway.
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