保存CLLocation错误:将变异方法发送到不可变对象 [英] Saving CLLocation error: Mutating method sent to immutable object
问题描述
我已经阅读了其他相关问题,但被卡住了.
I have read the other related questions, but I am stuck.
我正在尝试将最近的已知位置保存到plist中以供以后使用.
I am trying to save the last known location into a plist for later use.
这是我收到的错误消息:
Here is the error message I am receiving:
* 由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:-[__ NSCFDictionary setObject:forKey:]:发送给不可变对象的变异方法"
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
这是我的代码:
var plist = NSDictionary(contentsOfFile: NSBundle.mainBundle().pathForResource("Config", ofType: "plist"))
var dataToStore = NSKeyedArchiver.archivedDataWithRootObject(lastKnownLocation)
plist.setValue(dataToStore, forKey: "location")
"lastKnownLocation"变量是CLLocation. plist中的位置"键的类型为数据".有人可以帮忙,让我知道如何执行此操作(如果有更好的方法,请问他们如何执行此操作)?谢谢
The "lastKnownLocation" var is a CLLocation. The "location" key in the plist is of type "data". Could someone please assist and let me know how to do this (or how they do it if there is a better approach)? Thank you
推荐答案
您需要将NSDictionary
更改为NSMutableDictionary
.
var path = NSBundle.mainBundle().pathForResource("Config", ofType: "plist")
var plist: NSMutableDictionary = NSDictionary(contentsOfFile: path).mutableCopy() as NSMutableDictionary
这篇关于保存CLLocation错误:将变异方法发送到不可变对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!