let plistPath = NSBundle.mainBundle().pathForResource("userConfig", ofType: "plist")
let dict = NSMutableDictionary(contentsOfFile: plistPath)
let data: (AnyObject!) = dict.objectForKey("baiduUrl")
var myDictionary: String = "http://www.baidu.com"
myDictionary.setObject(plistPath, forKey: "baiduUrl")
myDictionary.writeToFile(plistPath, atomically: false)
println(myDictionary)
错误:'字符串'没有名为' setObject'
的成员
改变编码。我不能改变plist
答案 0 :(得分:1)
您将myDictionary声明为String(并使用String文字初始化)。鉴于名称,我很确定你的意思是将它声明为一个字典。字符串和字典不是一回事。
答案 1 :(得分:0)
var myDictionary: String = "http://www.baidu.com" // You say you have an String called myDictionary, not a dictionary
myDictionary.setObject(plistPath, forKey: "baiduUrl") // myDictionary is a String, so you can't set any object to it.
您需要做的是(未经测试,相关部分)
let myString:String = "http://www.baidu.com"
var myMutableDictionary = NSMutableDictionary()
myDictionary.setObject(myString, forKey:"baiduUrl")
或者如果您的字典是不可变的,您可以将其创建为NSDictionary,直接将myString分配给您的密钥。