0

CoreData code generation doesn't generate NSManagedObject subsclass for swift3 for example: It creates NSDate properties instead of Date. Any idea how can I generate models for Swift3?

Note:- I have found nothing in CoreData Code Generation settings to switch language explicitly to Swift 3 yet we can generate Swift models. But there are some classes which have been updated in Swift 3.

Umair Aamir
  • 1,624
  • 16
  • 26
  • Select the model file in the navigator, select an entity, press ⌥⌘1 and check if Code Generation > Language is set to Swift. – vadian May 09 '17 at 10:26
  • Code Generation Language is set to Swift but my question is "Why CodeGen converts all `Date` properties to `NSDate`?" – Umair Aamir May 09 '17 at 10:30
  • I know but I suspected that Xcode had generated Objective-C classes. – vadian May 09 '17 at 10:31
  • If I get it correctly, I think it is saying about different configurations of code generation. right? – Umair Aamir May 09 '17 at 11:07
  • Nevertheless simply change the type manually. You can also change NSNumber to Swift related types `Bool`, `Int32` etc. – vadian May 09 '17 at 13:08

1 Answers1

1

A lot of property types still use subclasses of NSObject. You will most likely need to convert them manually.

For NSDate specifically you may use:

return (date as NSDate?) ?? nil

and

return (date as Date?) ?? nil

For easier usage you can create extensions on Date and NSDate to return the typecast values the way you want it. An alternative is to change the date fields in database to be something like rawDate and then create a property date that overrides both setter and getter and does the conversion to/from original property rawDate.

In any case these objects are not excluded in Swift 3, they are just replaced with newer types in most of the API.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43