小弟调调
小弟调调

Reputation: 1333

Swift how to write plist file and change the value?

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)

Error : 'String' does not have a member named 'setObject'

enter image description here

change coding.I can not change plist enter image description here

Upvotes: 0

Views: 1985

Answers (2)

Carlos Pastor
Carlos Pastor

Reputation: 579

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.

What you need to do is (untested, relevant part)

let myString:String = "http://www.baidu.com"
var myMutableDictionary = NSMutableDictionary()
myDictionary.setObject(myString, forKey:"baiduUrl")

Or if your dictionary will be inmutable you can create it as NSDictionary assigning directly the myString to your key.

Upvotes: 0

Catfish_Man
Catfish_Man

Reputation: 41831

You declared myDictionary as a String (and initialized it with a String literal). I'm pretty sure you meant to declare it as a Dictionary, given the name. Strings and Dictionaries are not the same thing.

Upvotes: 1

Related Questions