I am building a dictionary using the contentsOfFile method, and pointing the method to a plist. My code is as follows:
func buildGraphicsDictionary() {
var theme: String = "defaultTheme"
var myDict: NSDictionary?
if let path = NSBundle.mainBundle().pathForResource(theme, ofType: "plist") {
println("path: \(path)")
myDict = NSDictionary(contentsOfFile: path)
println("dict count: \(myDict?.count)")
} else {
println("could not find plist file")
}
}
The plist is found. I know this for certain because I am able to print out the path and because when I change the name of the plist string to one that doesn't exist I get an error. So I am confident the problem is not in accessing the plist.
However, when I attempt to print the number of elements in the dictionary using count, the console reports that the myDict has a value of nil. So this tells me that the problem is actually in the method by which I am building the dictionary (contentsOfFile). What is confusing is that I have seen other SO articles in which the same code is used (ostensibly) successfully.
Any ideas what could be going on here?
I've tested your code and it works perfectly.
Maybe the problem in plist?
I tested your code with this plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>test1</key>
<string>one</string>
<key>test2</key>
<string>two</string>
</dict>
</plist>
And in my console I see:
dict count: Optional(2)
The plist must be located here
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments