Swift: Считать лист из MainBundle и записать напрямую в Documents не удается

Возьмите следующий файл с именемPermissions.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
    <key>SomeKey</key>
    <false/>
    </dict>
</plist>

Я хотел бы прочитать это из моегоMainBundle, измените его и запишите в мой.Documents, Однако, даже если я оставлю этонеизмененный, запись не удалась.Swift синтаксис, похоже, изменился сэтот вопроси другие вопросы, которые я мог найти, были вызваныневерные типы ключей, что было бы странно, учитывая, что я не изменяю перед записью. Вот полный код для воспроизведения ошибки:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Read in the plist from the main bundle.
        guard let path = NSBundle.mainBundle().pathForResource("Permissions", ofType: "plist") else {
            NSLog("Path could not be created.")
            return
        }

        guard NSFileManager.defaultManager().fileExistsAtPath(path) else {
            NSLog("File does not exist.")
            return
        }

        guard let resultDictionary = NSMutableDictionary(contentsOfFile: path) else {
            NSLog("Contents could not be read.")
            return
        }

        print(resultDictionary) // { Facebook = 0; }

        // Write it to the documents directory
        let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray

        guard let docsString = paths[0] as? String else {
            NSLog("Couldn't find documents directory; permissions could not be updated.")
            return
        }

        guard let docsURL = NSURL(string: docsString) else {
            NSLog("Couldn't convert the path to a URL; permissions could not be updated.")
            return
        }

        let plistURL = docsURL.URLByAppendingPathComponent("Permissions.plist")
        let plistPath = plistURL.path!
        let plistString = "\(plistURL)"

        if !resultDictionary.writeToURL(plistURL, atomically: false) {
            NSLog("Writing file to disk via url was unsucessful.") // Failure
        }

        if !resultDictionary.writeToFile(plistPath, atomically: false) {
            NSLog("Writing file to disk via path was unsucessful.")
        }

        if !resultDictionary.writeToFile(plistString, atomically: false) {
            NSLog("Writing file to disk via path was unsucessful.")
        }

        print("URL: ",NSMutableDictionary(contentsOfURL: plistURL)) // nil
        print("Path: ",NSMutableDictionary(contentsOfFile: plistPath)) // Prints
        print("String: ",NSMutableDictionary(contentsOfFile: plistString)) // Prints

    }

}

редактировать

Я сделал глупую логическую ошибку в исходном примере (отсутствует! в последней строке), который заставлял это выглядеть, как будто это терпело неудачу, когда это не было. Тем не менее, пример теперь терпит неудачу сURL метод, но работает либо сpath или жеString методы интерполяции. ПочемуURL метод не удался?

Ответы на вопрос(1)

Ваш ответ на вопрос