How to store custom objects in NSUserDefaults

后端 未结 7 1581
悲&欢浪女
悲&欢浪女 2020-11-21 11:48

Alright, so I\'ve been doing some poking around, and I realize my problem, but I don\'t know how to fix it. I have made a custom class to hold some data. I make objects fo

相关标签:
7条回答
  • 2020-11-21 12:23

    I create a library RMMapper (https://github.com/roomorama/RMMapper) to help save custom object into NSUserDefaults easier and more convenient, because implementing encodeWithCoder and initWithCoder is super boring!

    To mark a class as archivable, just use: #import "NSObject+RMArchivable.h"

    To save a custom object into NSUserDefaults:

    #import "NSUserDefaults+RMSaveCustomObject.h"
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults rm_setCustomObject:user forKey:@"SAVED_DATA"];
    

    To get custom obj from NSUserDefaults:

    user = [defaults rm_customObjectForKey:@"SAVED_DATA"]; 
    
    0 讨论(0)
提交回复
热议问题