将CLLocationDegrees更改为Double/NSNumber以保存在Core Data(Swift)中

前端之家收集整理的这篇文章主要介绍了将CLLocationDegrees更改为Double/NSNumber以保存在Core Data(Swift)中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在尝试将我的所有客观C代码更改为 Swift(这本身就是一个相当陡峭的学习曲线)时,我遇到了一个问题.

我只是想将CLLocationDegrees值保存到Core Data中.但我所做的一切都在起作用.

我开始时:

self.myLocation?.locationCurrentLat = self.fixedLocation?.coordinate.latitude

但是不知道如何让CLLocationDegrees向Double或NSNumber投降(如果这是正确的话),我在Google上搜索的任何东西都无济于事!

关于很多事情,我显然仍然模糊不清.这肯定是其中之一.

我可能做错了什么……或者需要做什么?

提前致谢

CLLocationDegrees是双倍的.你不应该做任何事情.

如果确实需要将其强制转换为double,请使用语法

Double(self.fixedLocation?.coordinate.latitude ?? 0)

要转换为NSNumber,您可以使用

NSNumber(value: self.fixedLocation?.coordinate.latitude ?? 0)

编辑:

我编辑了上面的代码,如果self.fixedLocation为nil,则使用“nil coalescing operator”给出值0.如果fixedLocation为nil,则返回包含nil的可选Int会更安全:

let latitude: Double?
if let location = self.fixedLocation {
  latitude =     Double(location.coordinate.latitude)
} else {
  latitude = nil
}
原文链接:https://www.f2er.com/swift/319012.html

猜你在找的Swift相关文章