3

我从 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 语句中,以下内容为零:

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

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

4

1 回答 1

2

问题:

正如@Steven Fisher在评论中所说。我的 .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)
            }

        }
    }
于 2015-09-06T00:57:55.157 回答