如果 Swift 中存在 NSUserDefault,则尝试更新 CLLocation
Posted
技术标签:
【中文标题】如果 Swift 中存在 NSUserDefault,则尝试更新 CLLocation【英文标题】:Trying to update CLLocation if NSUserDefault Exists in Swift 【发布时间】:2016-09-20 21:56:44 【问题描述】:最初我试图将 CLLocation 保存为 NSUserDefault 值,然后将其作为 CKRecord 存储在 CLoudkit 中,但出现错误:“defaults.setObject(locationRecord.recordID, forKey: "locationRecordID")”原因是“尝试将非属性列表对象设置为关键 locationRecordID 的 NSUserDefaults/CFPreferences 值”。所以现在我试图将 lat 和 long 作为默认值保存并替换 Cloudkit 中的旧位置。我目前在“publicDB.fetchRecordWithID((defaults.objectForKey("Location") as!CKRecordID),completionHandler:”行出现“Thread 1 SIGABRT”错误原因是“无法将 '__NSCFDictionary' (0x1a1bec968) 类型的值转换为 'CKRecordID'。”
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
let location = locations.last
self.loc1 = location!
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.004, longitudeDelta: 0.004))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
self.locationManager.startMonitoringSignificantLocationChanges()
self.getRecordToUpdate(locations)
let lat: CLLocationDegrees = center.latitude
self.lat1 = lat
let long: CLLocationDegrees = center.longitude
self.long1 = long
locationRecord.setObject(location, forKey: "location")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(locationRecord) record, error in
if error == nil
print("Location saved")
func getRecordToUpdate(locations:[CLLocation])
let defaults = NSUserDefaults.standardUserDefaults()
var locationRecord:CKRecord
if defaults.objectForKey("Location") == nil
locationRecord = CKRecord(recordType: "location")
let locationDict = ["lat": lat1, "lng": long1]//
defaults.setObject(locationDict, forKey: "Location")//
self.updateLocationRecord(locationRecord, locations: locations)
print("new")
else
let publicDB = CKContainer.defaultContainer().publicCloudDatabase
publicDB.fetchRecordWithID((defaults.objectForKey("Location") as! CKRecordID),completionHandler:
(record, error) in
if error == nil
self.updateLocationRecord(record!, locations: locations)
print("fetched record")
else
print("Error fetching previous record")
)
func updateLocationRecord(locationRecord:CKRecord, locations:[CLLocation])
let location = locations.last
locationRecord.setObject(location, forKey: "location")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(locationRecord) record, error in
if error == nil
print("Location saved")
【问题讨论】:
【参考方案1】:试试这个
您可以将 NSData 保存到 NSUserDefaults。因此,您只需将 CLLocation 对象转换为 NSData,如下所示:
// saving your CLLocation object
let locationData = NSKeyedArchiver.archivedDataWithRootObject(your location here)
NSUserDefaults.standardUserDefaults().setObject(locationData, forKey: "locationData")
// loading it
if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData")
if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation
println(loadedLocation.coordinate.latitude)
println(loadedLocation.coordinate.longitude)
【讨论】:
以上是关于如果 Swift 中存在 NSUserDefault,则尝试更新 CLLocation的主要内容,如果未能解决你的问题,请参考以下文章
如果 ref 不再存在,Swift -Firebase 如何触发 TransactionResult.abort()
IOS开发(Swift):如果value存在,则取当前数组索引的值
Swift:如果键不存在,则 decodeObjectForKey 崩溃
swift Swift Array Utils:如果元素不存在则附加元素,检索最新元素,...