swift - 将 float 保存到 NSUSERDEFAULT

标签 swift

我正在尝试保存坐标

(75.8572998 = 7 digits after decimal point)

NSUSERDEFAULT

但是当我取回它时,它只显示小数点后最多 3 位的值

i.e. 75.857.

在将其保存为用户默认值之前,我尝试将坐标转换为字符串或 float 。

但运气不好。

let locationValue:CLLocationCoordinate2D = manager.location!.coordinate
let latitude : Float = Float(locationValue.latitude)
print("location = \(latitude)")

NSUSERDEFAULT.set(latitude, forKey: "lat")
print(NSUSERDEFAULT.double(forKey: "lat"))


location = (30.9009991)
30.9009990692139

最佳答案

Swift 4.2 playground 测试代码以保存更大的数字,

let cor = 75.8572998232
UserDefaults.standard.set(cor, forKey: "cor")
let v = UserDefaults.standard.double(forKey: "cor")
print("Coordinate value is: \(v)")

输出

Coordinate value is: 75.8572998232

关于swift - 将 float 保存到 NSUSERDEFAULT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465690/

相关文章:

ios - 确定您的应用程序之前是否在 iOS 中安装过

swift - 最小化使用 LocationManager 时对我的服务器发出的 HTTP 请求

swift - 为什么转发可变参数无效?

ios - 从 Storyboard中的 tableview 单元格多次显示 segue

ios - Siri 完整句子顺序,例如通过 WhatsApp 发送消息

ios - 调用 dequeueReusableCell(withIdentifier :for:) swift 时内存泄漏

ios - 弹出演示 Controller (_ :willRepositionPopoverTo:in:) Trying to change the size of popUp on different orientation

swift - 将按钮添加到 NSTouchBar 控制条

ios - 快速检查 AVAudioPlayer 是否播放崩溃

swift - 如何在swift(FCM)中接收数据通知