将CLLocationDegrees更改为Double/NSNumber以保存在Core Data(Sw
发布时间:2020-12-14 05:23:07 所属栏目:百科 来源:网络整理
导读:在尝试将我的所有客观C代码更改为 Swift(这本身就是一个相当陡峭的学习曲线)时,我遇到了一个问题. 我只是想将CLLocationDegrees值保存到Core Data中.但我所做的一切都在起作用. 我开始时: self.myLocation?.locationCurrentLat = self.fixedLocation?.coord
在尝试将我的所有客观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 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |