Swift 4 - 无法指定“NSDate”类型的值来键入“Date?”仅适用于物理设备 [英] Swift 4 - Cannot assign value of type 'NSDate' to type 'Date?' ONLY on Physical Device
问题描述
我已经看到了与此错误相关的其他问题,但我没有看到有人提到我目前遇到的问题。请注意,在转换为Swift 4之前,我对此代码没有任何问题。
I've seen the other questions related to this error, but I've not seen anyone mention what I'm currently experiencing. Note that before converting to Swift 4, I had no problems with this code.
以下是有问题的代码:
let verseNotification = NSManagedObject(entity: entity, insertInto: self.managedObjectContext) as! VerseNotification
verseNotification.date_scheduled = NSDate(timeIntervalSinceNow: finalTimeToScheduleSinceNow)
请注意属性 date_scheduled
的类型为日期
:
@NSManaged public var date_scheduled: Date?
当我构建我的应用程序以在我的物理iPhone(iPhone 7)上的DEBUG中部署时,我得到了以下错误(从Swift 3转换为Swift 4之后):
When I build my app to deploy in DEBUG on my physical iPhone (an 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'
正如它所说的解决了问题...所以,现在我的代码是这样的:
Doing as it says fixes the problem... So, now my code is this:
verseNotification.date_scheduled = NSDate(timeIntervalSinceNow: finalTimeToScheduleSinceNow) as Date
...直到我将目标切换到模拟器。当我将目标切换为模拟器iPhone 7设备时,我现在收到以下错误:
...Until I switch the target to the simulator. When I switch the target to be the Simulator iPhone 7 device, I now get the following error:
'NSDate' is not implicitly convertible to 'Date'; did you mean to use 'as' to explicitly convert?
关于此错误的混淆是,正如您在上面所看到的,我明确转换为类型日期
。具有讽刺意味的是,我能为模拟器iPhone 7消除此错误的唯一方法是删除我在第一步中添加的作为日期
。
The confounding thing about this error is, as you can see above, I AM explicitly converting to type Date
. Ironically, the only way I can get rid of this error for the Simulator iPhone 7 is to remove the as Date
that I added in the first step.
发生了什么事?我缺少什么?
What is going on? What am I missing?
我担心为通用设备构建发布版本,不知道为什么我会收到相互冲突的错误。
I'm concerned about building a release version to "Generic Device," not knowing why I'm getting the conflicting errors.
更新
我发现了一些新的东西,非常有趣。
UPDATE I've discovered something new and quite interesting.
当我构建以部署到我的物理iPhone 7设备,生成的数据模型对象的Core Data定义将 date_scheduled
属性创建为 Date
。
When I build to deploy to my physical iPhone 7 device, the generated Core Data definition of my data model object creates the date_scheduled
attribute as Date
.
但是,当我构建部署在Simulator iPhone 7上时,生成的数据模型对象的Core Date定义会创建 date_scheduled
属性为NSDate。
However, when I build to deploy on the Simulator iPhone 7, the generated Core Date definition of my data model object creates the date_scheduled
attribute as NSDate.
这是Swift 4或Xcode 9中的新功能吗?从Swift 3转换到Swift 4后,我有没有新的设置?
Is this new in Swift 4 or Xcode 9? Is there a new setting that I'm missing after the conversion from Swift 3 to Swift 4?
推荐答案
这是Xcode的错误。请报告或打开雷达,以便在将来的版本中修复
It’s Xcode bug. Please, report or open radar on that to be fixed in future releases
这篇关于Swift 4 - 无法指定“NSDate”类型的值来键入“Date?”仅适用于物理设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!