Swift 3 - 无法转换日期类型的值?强制输入 'NSDate'
Swift 3 - Cannot convert value of type Date? to type 'NSDate' in coercion
我有一个全局 optional
CLLocation
变量如下:
var location : CLLocation? = nil
我想解包并将位置的 timestamp
转换为 NSDate
。
location?.timestamp as NSDate
我收到以下错误
Cannot convert value of type Date? to type 'NSDate' in coercion.
我该如何解决这个问题?
PS: 我是Swift 3.
的新手
您的 location
属性 是可选的,您正试图从中获取 timestamp
以便您获得可选的 Date?
实例,即您获得该实例的原因错误。
在 Swift 3 中使用 Date
而不是 NSDate
并使用 if let
或 guard
展开可选。
if let date = lastSavedLocation?.timestamp {
print(date)
}
我有一个全局 optional
CLLocation
变量如下:
var location : CLLocation? = nil
我想解包并将位置的 timestamp
转换为 NSDate
。
location?.timestamp as NSDate
我收到以下错误
Cannot convert value of type Date? to type 'NSDate' in coercion.
我该如何解决这个问题?
PS: 我是Swift 3.
的新手您的 location
属性 是可选的,您正试图从中获取 timestamp
以便您获得可选的 Date?
实例,即您获得该实例的原因错误。
在 Swift 3 中使用 Date
而不是 NSDate
并使用 if let
或 guard
展开可选。
if let date = lastSavedLocation?.timestamp {
print(date)
}