Search code examples
iosswiftnsobjectnskeyedarchiveruserdefaults

Trying to save custom object in UserDefaults using NSKeyedArchiver


I want to save my custom class object in userdefault. I'm trying to save it like this:

class func setModel<T: Hashable>(model: T, key: String) {
    let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: model)
    UserDefaults.standard.set(encodedData, forKey: key)
    UserDefaults.standard.synchronize()
}

but the app is crashing on the first line of this method with the error:

NSForwarding: warning: object 0x600003944000 of class 'DronesApp_Worker.WorkerProfileResponse' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[DronesApp_Worker.WorkerProfileResponse replacementObjectForKeyedArchiver:]

My custom object class is given below:

class WorkerProfileResponse: Codable, Hashable, Mappable{

    static func == (lhs: WorkerProfileResponse, rhs: WorkerProfileResponse) -> Bool {
        return lhs.id == rhs.id
    }

    override var hash: Int{
        return self.id!
    }

    var id, is_logged_in, last_login, last_active: Int?
    var username, email, mobile_number: String?
    var categoryName: String?
    var userCategories: [SelectedCategory]?
    var userSubCategories: [SelectedSubCategory]?
    var biometricToken: String?
    var accessToken: AccessToken?
    var userStatus: UserStatus?
    var userProfile: UserProfile?

    required init(map: Map) {

    }

    func mapping(map: Map) {
        id <- map["id"]
        is_logged_in <- map["is_logged_in"]
        last_login <- map["last_login"]
        last_active <- map["last_active"]
        biometricToken <- map["biometricToken"]
        username <- map["username"]
        email <- map["email"]
        mobile_number <- map["mobile_number"]
        accessToken <- map["accessToken"]
        userStatus <- map["userStatus"]
        userCategories <- map["userCategories"]
        userSubCategories <- map["userSubCategories"]
        userProfile <- map["userProfile"]
    }
}

Solution

  • Use JSONEncoder to save data to UserDefaults. Dummy code:

    class YourClassName: Codable {
        //Your properties here
    
        func saveInPreference() {
            do {
                let jsonEncoder = JSONEncoder()
                UserDefaults.standard.set(try jsonEncoder.encode(Your object here), forKey: "Write UserDefaultKey here")
                UserDefaults.standard.synchronize()
            }
            catch {
                print(error.localizedDescription)
            }
        }
    }
    

    Hope that helps!

    @Faraz A. Khan comment if you have any questions in the above piece of code.