Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto generate NSManagedObject subclasses with date attribute as Date instead of NSDate?

I'm currently updating my project to Swift 3 and I'm moving all my NSDate methods and extensions to Date in order to keep the standard in the app.

The problem is that I use Xcode to auto-generate my NSManagedObject subclasses and it is generating the date attributes as NSDate instead of Date.

Is there a way to generate it with the date attributes as Date?

EDIT

Per Apple Developer Documentation:

Core Data natively supports a variety of attribute types, such as string, date, and integer (represented as instances of NSString, NSDate and NSNumber respectively).

So I think it's not possible =/

like image 957
Bruno Guerios Avatar asked Nov 28 '16 21:11

Bruno Guerios


1 Answers

It's not currently possible, because Core Data is still very much tied to Objective-C types, and this is one of the places it shows.

However, you can still assign a Date to an NSDate attribute:

    newEvent.timestamp = Date() as NSDate

It's far from ideal, but if you have other code that uses Date, you don't have to make it use NSDate instead. Use as to convert only when working directly with your managed objects.

like image 185
Tom Harrington Avatar answered Nov 15 '22 09:11

Tom Harrington