在设备swift中存储CLLocation Manager值 [英] Store CLLocation Manager Values in Device swift

查看:70
本文介绍了在设备swift中存储CLLocation Manager值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序(每x秒)将位置值与一些额外的值(会话,ID等)发送给API,效果很好(请参见此处使用另一种方法更新CLLocation Manager )。但是为了提高功能,我们正在考虑设备可能会丢失(一段时间)互联网连接。因此,我需要临时存储所有值,并在重新连接后将它们再次发送到API。

I'm building an app that sends (every x seconds) to an API the location values with some extra values (session, ID, etc) that is working nice (see here Update CLLocation Manager on another method). But for improved feature, we are considering the device can lost (for some amount of time) internet connection. So I need to temporary store all values and when reconnected send them again to the API.

我正在考虑几种方法:


  • 核心数据(难以实现)

  • 领域(经验很少)

  • NSDisctionary

有人可以建议(并显示,如果可能的话)实现此功能的最佳方法吗?

Can anyone suggest (and show how, if possible) the best way to implement this feature?

推荐答案

如果要存储一些非敏感值(例如密码),建议使用 NSUserDefaults ,您可以像字典一样轻松地使用它:

If you want to store a some of non-sensitive values (such as a password), I suggest to use NSUserDefaults, you can easily use it like a dictionary:

注意:Swift 2代码。

例如:

    // shared instance (singleton)
    let userDefaults = NSUserDefaults.standardUserDefaults()

    // **storing**:
    let myString = "my string"
    userDefaults.setObject(myString, forKey: "kMyString")

    let myInt = 101
    userDefaults.setInteger(myInt, forKey: "kMyInt")

    let myBool = true
    userDefaults.setBool(myBool, forKey: "kMyBool")

    userDefaults.synchronize()

    // **retrieving**:
    //use optional binding for objects to check if it's nil
    if let myRetrievedString = userDefaults.objectForKey("kMyString") as? String {
        print(myRetrievedString)
    } else {
        // if it's nil
        print("there is now value for key: kMyString")
    }

    // if there is no value for key: kMyInt, the output should be zero by default
    let myRetrievedInt = userDefaults.integerForKey("kMyInt")

    // if there is no value for key: kMyBool, the output should be false by default
    let myRetrievedBool = userDefaults.boolForKey("kMyBool")

这篇关于在设备swift中存储CLLocation Manager值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆