swift3nsfilemanagernskeyedarchiverswift-dictionary

How to write Dictionary to a file?


I have a FileHelper class where I've implemented 3 methods whose job is to write a Dictionary contents to a file. Those methods are:

func storeDictionary(_ dictionary: Dictionary<String, String>, inFile fileName: String, atDirectory directory: String) -> Bool {
    let ext = "txt"
    let filePath = createFile(fileName, withExtension: ext, atDirectory: directory)
    /**** //If I use this method, file is created and dictionary is saved
    guard (dictionary as NSDictionary).write(to: filePath!, atomically: true) else {
        return false
    }
    */
    guard NSKeyedArchiver.archiveRootObject(dictionary, toFile: (filePath?.absoluteString)!) else {
        return false
    }
    return true
}
func createFile(_ file: String, withExtension ext: String, atDirectory directory: String) -> URL? {
    let directoryPath = createDirectory(directory)
    let filePath = directoryPath?.appendingPathComponent(file).appendingPathExtension(ext)

    if !FileManager.default.fileExists(atPath: (filePath?.absoluteString)!) {
        let success = FileManager.default.createFile(atPath: (filePath?.absoluteString)!, contents: nil, attributes: nil)
        print("\(success)") //** here is the issue I investigated. Always prints false.
    }

    return filePath
}
func createDirectory(_ directory: String) -> URL? {
    let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let directoryPath = documentsDirectory.appendingPathComponent(directory)

    do {
        try FileManager.default.createDirectory(at: directoryPath, withIntermediateDirectories: true, attributes: nil)
    } catch let error as NSError {
        fatalError("Error creating directory: \(error.localizedDescription)")
    }
    return directoryPath
}

When I call FileHelper().storeDictionary(aValidDictionary, inFile: "abc", atDirectory: "XYZ") to write the dictionary, it fails with this procedure. But if I use

guard (dictionary as NSDictionary).write(to: filePath!, atomically: true) else {
    return false
}

it works.

What's wrong with NSKeyedArchiver.archiveRootObject(_:toFile:) method??

And why FileManager.default.createFile(atPath: (filePath?.absoluteString)!, contents: nil, attributes: nil) always returns false?


Solution

  • First of all filePath?.absoluteString returns the entire – even percent escaped – string including the file:// scheme and the method expects a path without the scheme (filePath?.path - the naming is a bit confusing ;-) ).

    I recommend to save a [String:String] dictionary as property list file. It's not necessary to create the file explicitly.

    I changed the signatures of the methods slightly in the Swift-3-way. Further there is no need to use any optional type.

    func store(dictionary: Dictionary<String, String>, in fileName: String, at directory: String) -> Bool {
        let fileExtension = "plist"
        let directoryURL = create(directory:directory)
        do {
            let data = try PropertyListSerialization.data(fromPropertyList: dictionary, format: .xml, options: 0)
            try data.write(to: directoryURL.appendingPathComponent(fileName).appendingPathExtension(fileExtension))
            return true
        }  catch {
            print(error)
            return false
        }
    }
    
    func create(directory: String) -> URL {
        let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let directoryURL = documentsDirectory.appendingPathComponent(directory)
    
        do {
            try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
        } catch let error as NSError {
            fatalError("Error creating directory: \(error.localizedDescription)")
        }
        return directoryURL
    }
    

    PS: Instead of returning a Bool you could make the store method can throw and handle the error in the calling method:

    func store(dictionary: Dictionary<String, String>, in fileName: String, at directory: String) throws {
        let fileExtension = "plist"
        let directoryURL = create(directory:directory)
    
        let data = try PropertyListSerialization.data(fromPropertyList: dictionary, format: .xml, options: 0)
        try data.write(to: directoryURL.appendingPathComponent(fileName).appendingPathExtension(fileExtension))
    }