0

I have an existing iOS app that uses CoreData. I'm adding the CoreData classes to the existing watchOS app. (The watchOS app already existed, but had no CoreData previously.)

I'm finding that Date attributes are Swift Dates in watchOS, but NSDates in watchOS. This is baffling to me. For example, this is how I seemingly need to write this code (which I'd like to share across targets):

    let theDate = Date() //Simplified example
    #if os(watchOS)
        myEntity.myAttributeDate = theDate as NSDate
    #elseif os(iOS)
        myEntity.myAttributeDate = theDate
    #endif

If I remove the build check, I'll get an error in one platform or the other (depending on whether the coercion is there). "Cannot assign value of type Date to type NSDate" is what shows up in watchOS if I don't coerce.

Is this a known issue? Is there a setting to work around this somehow? I fear a huge hot mess of #if's in my code...

Mitch Cohen
  • 1,551
  • 3
  • 15
  • 29
  • (I do recognize I could simplify the #if/#else/#endif thing by using a class extension to coerce my dates. This still seems crazy.) – Mitch Cohen Nov 14 '17 at 00:47
  • Seems the +CoreDataProperties.swift extension file is being generated for the iOS target, but not the watchOS target. If/when I figure out why, I'll update the post... – Mitch Cohen Nov 14 '17 at 01:48

0 Answers0