从.plist读取总是返回nil

时间:2015-09-05 23:12:33

标签: swift plist nsfilemanager nsbundle xcode6.4

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

我已点击以下内容添加了words.plist

{my project}>目标>构建阶段>复制捆绑资源

enter image description here

然后我在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)
        }   
    }
}

enter image description here

问题

为什么我得到一个零值并且有什么方法可以添加一个plist文件并从中读取?

更新:

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

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

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

1 个答案:

答案 0 :(得分:2)

问题:

正如@Steven Fisher所述,在评论中。我的.plist文件是数组,而不是 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)
            }

        }
    }