1

I am attempting to use the NSKeyedArchiver to write a Codable to disk.

All the questions I could find on the subject using deprecated methods. I can't find any SO questions or tutorials using the Swift 4 syntax.

I am getting the error

-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 

Which I am guessing is the try writeData.write(to: fullPath) line in my UsersSession class.

What is the proper way to write data to a file in Swift 4.2?

struct UserObject {
    var name : String?
}

extension UserObject  : Codable {

    enum CodingKeys : String, CodingKey {
        case name
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        name = try container.decode(String.self, forKey: .name)

    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(name, forKey: .name)
    }
}

UserSession.swift

class UserSession {
    static let shared = UserSession()

    let fileName = "userdata.dat"

    var user : UserObject?

    lazy var fullPath : URL =  {
        return getDocumentsDirectory().appendingPathComponent(fileName)
    }()

    private init(){
        print("FullPath: \(fullPath)")
        user = UserObject()
        load()
    }

    func save(){
        guard let u = user else { print("invalid user data to save"); return}
        do {
            let writeData = try NSKeyedArchiver.archivedData(withRootObject: u, requiringSecureCoding: false)
            try writeData.write(to: fullPath)
        } catch {
            print("Couldn't write user data file")
        }

    }

    func load() {
        guard  let data = try? Data(contentsOf: fullPath, options: []) else {
            print("No data found at location")
            save()
            return
        }

        guard  let loadedUser = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? UserObject else {
            print("Couldn't read user data file.");
            return
        }

        user = loadedUser

    }

    func getDocumentsDirectory() -> URL {
        return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    }
}
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • You are mixing up `NSCoding` and `Codable`. Somebody reopened the duplicate but it **is** a duplicate. Please see https://stackoverflow.com/questions/51812407/how-to-save-list-of-object-in-user-default/51816174#51816174. My answer describes the difference between the protocols and how to use it. Writing the data to `UserDefaults` or to a file is irrelevant. – vadian Nov 04 '18 at 11:54

1 Answers1

1

Since you are using Codable, you should first encode to Data and then archivedData. Here is the code:

   func save(){
        guard let u = user else { print("invalid user data to save"); return}
        do {
            // Encode to Data
            let jsonData = try JSONEncoder().encode(u)
            let writeData = try NSKeyedArchiver.archivedData(withRootObject: jsonData, requiringSecureCoding: false)
            try writeData.write(to: fullPath)
        } catch {
            print("Couldn't write user data file")
        }

    }

    func load() {
        guard  let data = try? Data(contentsOf: fullPath, options: []) else {
            print("No data found at location")
            save()
            return
        }

        guard  let loadedUserData = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? Data else {
            print("Couldn't read user data file.");
            return
        }
        // Decode Data
        user = try? JSONDecoder().decode(UserObject.self, from: loadedUserData)

    }
Hanson
  • 11
  • 3