gpt4 book ai didi

swift - 从 .plist 读取总是返回 nil

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:02 24 4
gpt4 key购买 nike

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

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

{my project} > targets > build phases > copy Bundle Resources

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

问题

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


更新:

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

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

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

最佳答案

问题:

作为@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)
}

}
}

关于swift - 从 .plist 读取总是返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32418526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com