简体   繁体   中英

iOS Swift Storing Realm Object to UserDefaults.standard

Here is my Realm Object:

PracticeAreaEntity {
    id = 0;
    c_id = 21871;
    c_number = C.C/1/2016;
    c_title = Title Here;
    date = 2017-04-07T00:00:00.000Z;
    description2 = ;
    object = {"id":205039,"c_id":21871,"name":"07-04-2017","s_time":"2017-04-07T00:00:00.000Z","e_time":"2017-04-08T00:00:00.000Z","creator":"system","created_by_id":null,"description":null,"c_at":"2017-03-26T00:11:52.134Z","u_at":"2017-04-07T01:00:27.396Z","judge":null,"phearing":null,"google_event_id":null,"is_completed":false,"business":null,"last_notified":"2017-04-07T01:00:27.382Z","ng_date":null,"b_date":null,"b_d_available":false};
    p_id = 205039;
    subject = 07-04-2017;
    type = Event;
}

I need to store this realm object to UserDefault.standard and get values back in another page. And here is my UserDefults code:

let userDefaults = UserDefaults.standard
userDefaults.set(filteredtotalContactArray[indexPath.row], forKey: "SelectedPractice")
userDefaults.synchronize()

filteredtotalContactArray[indexPath.row] equals to values that I have specified before as Realm Object. But I am getting error as

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object 

You'll need to supply conformance methods to NSCoding for your Realm models in order to be able to persist them to UserDefaults .

However, I'd encourage you to use Realm to persist Realm models given that you're already using Realm in your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM