【问题标题】:Swift how to write plist file and change the value?Swift如何编写plist文件并更改值?
【发布时间】:2014-08-14 15:52:20
【问题描述】:
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

【问题讨论】:

    标签: objective-c xcode macos swift


    【解决方案1】:

    您将 myDictionary 声明为 String(并使用 String 文字对其进行了初始化)。我很确定您打算将其声明为字典,给定名称。字符串和字典不是一回事。

    【讨论】:

    • 已声明 myDictionary。仍然无法写入 Plist。
    • 请用新错误和新代码更新问题。
    • 更改编码。我无法更改 plist
    【解决方案2】:
    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 分配给您的键。

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多