Why is an NSDate in a Core Data managed Object converted to NSTimeInterval?

后端 未结 3 392
别跟我提以往
别跟我提以往 2020-12-09 16:48

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

相关标签:
3条回答
  • 2020-12-09 16:54

    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.

    0 讨论(0)
  • 2020-12-09 16:54

    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.

    0 讨论(0)
  • 2020-12-09 17:07

    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.

    0 讨论(0)
提交回复
热议问题