7

Xcode 9 generates different code for Date type attribute of the entity in simulator vs device. I have codegen feature under Class set to category/extension in coredata.

Until Xcode 8.3 (latest) it was all working fine (NSDate always). Below is the auto generated code by Xcode 9 (Swift 4) for the attribute -

On Device:-

@NSManaged public var requiredDate: Date?

AND,

On Simulator:-

@NSManaged public var requiredDate: NSDate?

enter image description here

Anyone encountered this problem? What is the best solution for a project with 50+ members to fix this until an Xcode update fix it (I hope there is an apple radar for this)?

Ashok
  • 6,034
  • 1
  • 34
  • 50
  • Actually even in Swift 3 the recommended class (struct) is `Date` – vadian Sep 22 '17 at 20:08
  • I assume you are just commenting your observation, not suggesting or asking anything. Note, the code I pasted above is *auto generated* by Xcode since codegen setting is NOT "Manual/None". Updated my question language to be more clear. – Ashok Sep 22 '17 at 20:29
  • I'm suggesting the compiler is not perfect, don't trust it blindly. For example even if you declare a Core Data attribute as non-optional the compiler generates optional properties regardless, and the compiler generates `NSNumber` for any numeric type although Swift `Int`, `Double`, `Bool` is perfectly valid, too. – vadian Sep 22 '17 at 20:43
  • Yeah, true. I don't know what is the best way to resolve this. Don't want to use "Manual/None" setting. And, if I use #if macro and handle this attribute differently on device vs simulator then I'm worried when Xcode will fix this in an update then the whole team + automation servers needs update Xcode all at almost same time. Treat it as major Xcode update. Not a good solution. – Ashok Sep 22 '17 at 20:58
  • There is some **randomness** in this issue. Suddenly, the issue has disappeared automatically and `codegen` has settled on Date on both simulator and device. – Ashok Sep 24 '17 at 02:00

1 Answers1

6

Let me answer this myself. These are my observations (so far) and potential solution.

This issue seems RANDOM. Suddenly, the issue has disappeared and codegen has finally settled on Date on both simulator and device.

However, I applied macro based solution (and now no longer needed) to solve it -

// Workaround for Xcode 9 bug. The autogenerated code for 'Date' type attribute is NSDate vs Date based on device vs simualtor.

// This macro condition should be removed once an Xcode update fixes this issue
#if (arch(i386) || arch(x86_64))    // Simulator
    requiredDate <- (map["requiredDate"], NSDateTransform())    // milliseconds to NSDate
#else   // Device
    requiredDate <- (map["requiredDate"], DateTransform())    // milliseconds to Date
#endif

PS: I remember I tested it working at least on iPhone SE Simulator, iPhone 7 device

Ashok
  • 6,034
  • 1
  • 34
  • 50
  • Please report this to Apple's bug reporter: https://bugreport.apple.com – Tom Harrington Sep 25 '17 at 22:22
  • As told in my answer this issue is strangely random. I came across this issue in our existing project after Xcode 9.0. I applied the workaround mentioned in this answer then it was all good for one day. Then suddenly, compiler again complained (build error) so I ended up removing the workaround, settling on `Date` for all. Never saw this issue again after that. – Ashok Nov 23 '17 at 06:04
  • Yeah, where would we put this code, @Ashok? Thanks. This problem is utterly ridiculous. 3 Months and still not fixed. – Dakine83 Dec 18 '17 at 01:20
  • I have this problem too, but it didn't magically go away for me. I've submitted a bug to Apple. – Kenny Wyland Jan 23 '18 at 19:01
  • I ran into this with XCode 9.2 (9C40b), but ultimately just needed to run Clean to get rid of all the old generated code. – superstator Feb 19 '18 at 19:19