使用 NSUserDefaults 存储自定义对象 JSONMODEL

1、使用 NSUserDefaults存储自定义对象

NSArray *arr = [[NSArray alloc] initWithArray: json对象数组  ];

//使用NSKeyedArchiver 归档

NSData *ContactMulti = [NSKeyedArchiver archivedDataWithRootObject:arr];

NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

[userDefault setObject:ContactMulti forKey:@"ContactMulti"];

[userDefault synchronize];


2、获取NSUserDefaults存储的自定义对象

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

NSData *data = [userDefaults objectForKey:@"ContactMulti"];

self.currentChooseContacts = [NSMutableArray array];

////使用NSKeyedUnarchiver 反归档

self.currentChooseContacts = [NSKeyedUnarchiver unarchiveObjectWithData:data];


3、jsonmodel 对象

@interface ContactsModel : JSONModel

/// @brief 联系人Id

@property (strong,nonatomic) NSString *contactId;

/// @brief 部门Id

@property (strong,nonatomic) NSString *orgId;

/// @brief 联系人头像(图片名)

@property (strong,nonatomic) NSString *headImg;

/// @brief 联系人名字

@property (strong,nonatomic) NSString *usersName;

@end

你可能感兴趣的:(使用 NSUserDefaults 存储自定义对象 JSONMODEL)