NSDictionary using contentsOfFile method doesn't build in Swift

zeeple

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?

imike

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

Plist place

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ContentsOfFile method deprecated in swift 2.2

Swift: can't load file using String(contentsOfFile:encoding:)

build method doesn't wait for async

Access data in NSDictionary using Swift

UIImage using contentsOfFile

AVAudioPlayerDelegate swift doesn't call the method

Swift Firebase Doesn't invoke method of observeSingleEvent

Call specific number if NSDictionary doesn't match

Flutter setState() doesn't call always call my build method

How to Read Plist without using NSDictionary in Swift?

Trouble using NSDictionary convenience init in Swift

Swift: UIStackView of UIControls with selector method that doesn't fire

IOHIDDeviceSetReport method doesn't accept UnsafeRawPointer type in Swift 3

Swift doesn't recognize method from Obj-C class

method doesn't override from its superclass- swift

Using git bisect to find the latest commit that doesn't build

doesn't display build in apple store using xcode 8

Flutter doesn't build iOS app using Firestore

CSS media query doesn't work after using react build

How do I use String(contentsOfFile: ) in Swift?

Build expression tree for LINQ using List<T>.Contains method

NSDictionary to NSData and NSData to NSDictionary in Swift

UseState called by UseEffect doesn't update the variable using the Set method

Scala: generic method using implicit evidence doesn't compile

Why doesn't setPos() work when using addPolygon() method?

Mockito: using a method in "thenReturn" to return a mock doesn't work

Object doesn't seems to collide using AABB collision method

mocking a method using Moq framework doesn't return expected result

Using Arrays.toString on a method output, but it doesn't seem to convert