`NSBundle.mainBundle (). URLForResource` always returns `nil`

I am new to Swift and am using Xcode 6.

I am trying to read data from an app file plist, but it does not work.

The file is data.plistincluded in the Xcode group Supporting Files.

I am using the following code:

var dataList = NSDictionary(contentsOfURL:NSBundle.mainBundle().URLForResource("data", withExtension:"plist"))

however NSURL:

NSBundle.mainBundle().URLForResource("data", withExtension:"plist")

always returns nil.

I do not know what is wrong.

+4
source share
1 answer

. plist, , . , , . plist, . , , , , .

var path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
    path = path.stringByAppendingPathComponent("data.plist")
    let fileManager = NSFileManager.defaultManager()
    if !fileManager.fileExistsAtPath(path) {
        let sourcePath = NSBundle.mainBundle().pathForResource("data", ofType: "plist")
        fileManager.copyItemAtPath(sourcePath, toPath: path, error: nil)
}

.

let dict = NSMutableDictionary(contentsOfFile: path) as NSMutableDictionary
+1

All Articles