在设备中存储 CLLocation 管理器值 swift
Store CLLocation Manager Values in Device swift
我正在构建一个应用程序,它(每 x 秒)向 API 位置值发送一些额外的值(会话、ID 等),效果很好(参见此处 Update CLLocation Manager on another method).但为了改进功能,我们正在考虑设备可能会丢失(一段时间)互联网连接。所以我需要临时存储所有值,并在重新连接时再次将它们发送到 API.
我正在考虑几种方法:
- 核心数据(难实现)
- 境界(经验很少)
- NSDictionary
任何人都可以建议(并在可能的情况下展示如何)实现此功能的最佳方法吗?
如果你想存储一些非敏感值(比如密码),我建议使用NSUserDefaults,你可以像字典一样轻松使用它:
注:Swift2码.
例如:
// 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")
Tadaaaaaa:
func arrayOfDictionaries() {
var offline:[[String:AnyObject]] = []
offline.append(["LATITUDE: ": userLocation.coordinate.latitude, "LONGITUDE: ": userLocation.coordinate.longitude, "SPEED: ": userLocation.speed])
NSUserDefaults().setObject(offline, forKey: "offLine")
if let offLinePositions = NSUserDefaults().arrayForKey("offLine") as? [[String:AnyObject]] {
//print(offLinePositions)
for item in offLinePositions {
print(item["LATITUDE: "]! as! NSNumber) // A, B
print(item["LONGITUDE: "]! as! NSNumber) // 19.99, 4.99
print(item["SPEED: "]! as! NSNumber) // 1, 2
}
}
}
我正在构建一个应用程序,它(每 x 秒)向 API 位置值发送一些额外的值(会话、ID 等),效果很好(参见此处 Update CLLocation Manager on another method).但为了改进功能,我们正在考虑设备可能会丢失(一段时间)互联网连接。所以我需要临时存储所有值,并在重新连接时再次将它们发送到 API.
我正在考虑几种方法:
- 核心数据(难实现)
- 境界(经验很少)
- NSDictionary
任何人都可以建议(并在可能的情况下展示如何)实现此功能的最佳方法吗?
如果你想存储一些非敏感值(比如密码),我建议使用NSUserDefaults,你可以像字典一样轻松使用它:
注:Swift2码.
例如:
// 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")
Tadaaaaaa:
func arrayOfDictionaries() {
var offline:[[String:AnyObject]] = []
offline.append(["LATITUDE: ": userLocation.coordinate.latitude, "LONGITUDE: ": userLocation.coordinate.longitude, "SPEED: ": userLocation.speed])
NSUserDefaults().setObject(offline, forKey: "offLine")
if let offLinePositions = NSUserDefaults().arrayForKey("offLine") as? [[String:AnyObject]] {
//print(offLinePositions)
for item in offLinePositions {
print(item["LATITUDE: "]! as! NSNumber) // A, B
print(item["LONGITUDE: "]! as! NSNumber) // 19.99, 4.99
print(item["SPEED: "]! as! NSNumber) // 1, 2
}
}
}