保存CLLocation错误:发送到不可变对象的变异方法
我已经读了其他相关的问题,但我卡住了。
我正在尝试将最后一个已知位置保存到plist中以备后用。
这是我收到的错误消息:
*终止应用程序由于未捕获的exception'NSInternalInconsistencyException',原因:' – [__ NSCFDictionary setObject:forKey:]:mutating方法发送到不可变对象'
这是我的代码:
var plist = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("Config", ofType: "plist")) var dataToStore = NSKeyedArchiver.archivedDataWithRootObject(lastKnownLocation) plist.setValue(dataToStore, forKey: "location")
“lastKnownLocation”var是一个CLLocation。 plist中的“位置”键是“数据”types。 有人可以请帮助,让我知道如何做到这一点(或如果有更好的办法他们如何做)? 谢谢
您需要将NSDictionary
更改为一个NSMutableDictionary
。
var path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist") var plist: NSMutableDictionary = NSDictionary(contentsOfFile: path).mutableCopy() as NSMutableDictionary
plist.setValue(dataToStore, forKey: "location")
你不能在NSDictionary
上调用setValue
,因为它是一个变异的方法, NSDictionary
是不可变的。
var plist = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("Config", ofType: "plist"))
将NSDictionary
更改为NSMutableDictionary
。
- FBFriendPickerViewController在iOS上显示和清空表格。 放置选取器运行良好
- 在UIManagedDocuments获取新数据后,NSFetchedResultsController不会更新TableView
- 使用当前位置数据时,CLGeocoder返回城市和州的空结果
- 计算总旅行距离iOS Swift
- 使用Geocoder更新属性来获取用户实际地址EXC_BAD_ACCESS
- 更新从CLGeocoder返回的CLPlacemark的MKMapView
- didFailWithError:错误域= kCLErrorDomain代码= 0“操作无法完成。 (kCLErrorDomain错误0.)“
- IOS应用程序终止时获取位置更新,而不使用significantChange
- -distanceFromLocation中CLLocation对象的顺序:
- 在ios 6和ios 9之间的CLLocation后台服务启用变化
- 使用CLLocation计算两个坐标之间的距离