【问题标题】:reading from .plist always returns nil从 .plist 读取总是返回 nil
【发布时间】:2015-12-01 19:41:30
【问题描述】:

我从 plist 读取的所有尝试都导致返回 nil 值,我已经在 Xcode 6 和 Xcode beta 7 上以多种方式进行了尝试。此外,还有很多堆栈上有几个类似的问题,我已经尝试了很多,但没有一个能解决这个问题。

我已经通过点击添加了我的words.plist

{我的项目} > 目标 > 构建阶段 > 复制捆绑资源

然后我在 ViewController 中尝试了以下代码的几种变体:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
    let documentsDirectory = paths[0] as! String
    let path = documentsDirectory.stringByAppendingPathComponent("words.plist")

    let fileManager = NSFileManager.defaultManager()

    //check if file exists
    if(!fileManager.fileExistsAtPath(path)) {
        // If it doesn't, copy it from the default file in the Bundle
        if let bundlePath = NSBundle.mainBundle().pathForResource("words", ofType: "plist") {

            let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
            println("Bundle words file is --> \(resultDictionary?.description)") // this is nil!!!

            fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)

        } else {
            println("words not found. Please, make sure it is part of the bundle.")
        }
    } else {
        println("words already exits at path.")
        // use this to delete file from documents directory
        //fileManager.removeItemAtPath(path, error: nil)

    }
    print("entering if-let")
    if let pfr = NSBundle.mainBundle().pathForResource("words", ofType: "plist") {
        print("\nin let\n")
        print(pfr)
        print("\nentering dict if-let\n")
        if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject> {
            // use swift dictionary as normal
            print("\nin let\n")
            print(dict)
        }   
    }
}

问题

为什么我得到一个 nil 值以及添加 plist 文件并从中读取的正确方法是什么?


更新:

在我的 if 语句中,以下是 nil:

let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)
println("Bundle words file is --> \(resultDictionary?.description)") // this is nil!!!

对我来说,这表明 Xcode 不知道我的 words.plist 文件,或者我将 bundlePath 指向了错误的位置。

【问题讨论】:

  • 各种println 语句的完整输出是什么?请告诉我们实际发生了什么
  • @matt,所以我更新了我的问题。看起来“resultDictionary?.description”以 nil 开头,这表明我的路径没有指向我的 words.plist 文件。
  • 你的 words.plist 可能是一个数组而不是字典。
  • 你是对的......我会尝试使用数组
  • 糟糕!我的错!我现在就这样做

标签: swift plist nsfilemanager nsbundle xcode6.4


【解决方案1】:

问题:

正如@Steven Fisher 所说,在 cmets 中。我的 .plist 文件是 Array 而不是 NSDictionary。所以我只需要从我的代码中切换两行:

let resultDictionary = NSMutableDictionary(contentsOfFile: bundlePath)

let resultDictionary = NSMutableArray(contentsOfFile: bundlePath)

还有

if let dict = NSDictionary(contentsOfFile: path) { //...

if let dict = NSArray(contentsOfFile: path) { //..

最终工作代码:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
        let documentsDirectory = paths[0] as! String
        let path = documentsDirectory.stringByAppendingPathComponent("words.plist")

        let fileManager = NSFileManager.defaultManager()

        //check if file exists
        if(!fileManager.fileExistsAtPath(path)) {
            // If it doesn't, copy it from the default file in the Bundle
            if let bundlePath = NSBundle.mainBundle().pathForResource("words", ofType: "plist") {

                let resultDictionary = NSMutableArray(contentsOfFile: bundlePath)
                println("Bundle words file is --> \(resultDictionary?.description)")

                fileManager.copyItemAtPath(bundlePath, toPath: path, error: nil)

            } else {
                println("words not found. Please, make sure it is part of the bundle.")
            }
        } else {
            println("words already exits at path.")
            // use this to delete file from documents directory
            //fileManager.removeItemAtPath(path, error: nil)

        }
        print("entering if-let")
        if let pfr = NSBundle.mainBundle().pathForResource("words", ofType: "plist") {
            print("\nin let\n")
            print(pfr)
            print("\nentering dict if-let\n")
            if let dict = NSArray(contentsOfFile: pfr) {
                // use swift dictionary as normal
                print("\nin let\n")
                print(dict)
            }

        }
    }

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 2015-03-05
    • 2014-08-25
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多