[英]why I cannot read data from plist?
This is part my code. 这是我的代码的一部分。 But
myDict
is nil
. 但是
myDict
是nil
。 The filename is right.I already checked for many times. 文件名正确。我已经检查了很多次。
var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource("CatData", ofType: "plist") {
myDict = NSDictionary(contentsOfFile: path)
}
if let dict = myDict {
print("print something")
}
your plist is array of dictionary so try 您的plist是字典数组,请尝试
var myArray: NSArray?
if let path = NSBundle.mainBundle().pathForResource("Categories", ofType: "plist") {
myArray = NSArray(contentsOfFile: path)
}
if let array = myArray {
for item: AnyObject in array {
if let item = item as? NSDictionary {
if let categoryTitle = item["CategoryTitle"] as? NSString {
print("categoryTitle = ", categoryTitle)
}
if let imageNames = item["ImageNames"] as? NSArray {
print("imageNames = ", imageNames)
}
}
}
}
Providing your file name is correct, and your plist has been added to the bundle: 提供正确的文件名,并且您的plist已添加到捆绑软件中:
You need to make sure your file is the type you're expecting. 您需要确保文件是您期望的类型。
For example. 例如。 The root of your plist is an Array, and you're looking to get a Dictionary out of it.
plist的根是一个数组,您正在寻找从中获取字典的方法。
So lets rewrite your code: 因此,让我们重写您的代码:
var myArray: Array? {
if let path = NSBundle.mainBundle().pathForResource("CatData", ofType: "plist") {
myArray = NSArray(contentsOfFile: path) as! [[String:AnyObject]]
}
if let array = myArray {
print("print something")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.