Swift 4 - Unable to set value to type "NSDate" for input "Date?" On a physical device ONLY

I saw other questions related to this error, but I did not see anyone mentioning what I am experiencing right now. Please note that before converting to Swift 4, I had no problems with this code.

The following is the violation code:

let verseNotification = NSManagedObject(entity: entity, insertInto: self.managedObjectContext) as! VerseNotification
verseNotification.date_scheduled = NSDate(timeIntervalSinceNow: finalTimeToScheduleSinceNow)

Note that the attribute date_scheduledis of type Date:

@NSManaged public var date_scheduled: Date?

When I create a deployment application in DEBUG on my physical iPhone (iPhone 7), I get the following error (after converting from Swift 3 to Swift 4):

Cannot assign value of type 'NSDate' to type 'Date?' - Insert 'as Date'

Execution, as they say, fixes the problem ... So now my code is this:

verseNotification.date_scheduled = NSDate(timeIntervalSinceNow: finalTimeToScheduleSinceNow) as Date

... until I switch the target to the simulator. When I switch the target to the Simulator iPhone 7 device, now I get the following error:

'NSDate' is not implicitly convertible to 'Date'; did you mean to use 'as' to explicitly convert?

, , AM Date. , Simulator iPhone 7 - as Date, .

? ?

, "Generic Device", , .

UPDATE - .

iPhone 7, Core Data date_scheduled Date.

, Simulator iPhone 7, Core Date date_scheduled NSDate.

Swift 4 Xcode 9? , Swift 3 Swift 4?

+6
2

Xcode. , , .

+2

(Xcode 9.2, Swift 4.0, watchOS 4):

#if arch(i386) //watchOS Simulator needs this
    completionDate = newValue as NSDate?
#else //watchOS device needs this
    completionDate = newValue
#endif  

iOS watchOS, :

#if os(watchOS)
    #if arch(i386) //watchOS Simulator needs this
            completionDate = newValue as NSDate?
        #else //watchOS device needs this
            completionDate = newValue
    #endif  
#else //iOS, simulator or device
        completionDate = newValue
#endif

, finalDate - Date Data Date, newValue - Swift "Date?" .

0

All Articles