Dar*_*ren 7 core-location swift
在尝试将我的所有客观C代码更改为Swift(这本身就是一个相当陡峭的学习曲线)时,我遇到了一个问题.
我只是想将CLLocationDegrees值保存到Core Data中.但我所做的一切都在起作用.
我开始时:
self.myLocation?.locationCurrentLat = self.fixedLocation?.coordinate.latitude
Run Code Online (Sandbox Code Playgroud)
但是不知道如何让CLLocationDegrees向Double或NSNumber投降(如果这是正确的话),我在Google上搜索的任何东西都无济于事!
关于很多事情,我显然仍然模糊不清.这肯定是其中之一.
我可能做错了什么......或者需要做什么?
提前致谢
Dun*_*n C 19
CLLocationDegrees 是双倍的.你不应该做任何事情.
如果确实需要将其强制转换为double,请使用语法
Double(self.fixedLocation?.coordinate.latitude ?? 0)
Run Code Online (Sandbox Code Playgroud)
要转换为NSNumber,您可以使用
NSNumber(value: self.fixedLocation?.coordinate.latitude ?? 0)
Run Code Online (Sandbox Code Playgroud)
我编辑了上面的代码,使用"nil coalescing operator"给出值0,如果self.fixedLocation是nil.如果fixedLocation为零,则返回包含nil的可选Int会更安全:
let latitude: Double?
if let location = self.fixedLocation {
latitude = Double(location.coordinate.latitude)
} else {
latitude = nil
}
Run Code Online (Sandbox Code Playgroud)
以下是如何在Objective-C中转换为NSNumber;
[NSNumber numberWithDouble:newLocation.coordinate.longitude]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11295 次 |
| 最近记录: |