由于NSUserDefaults存储类型有限(NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL.),不支持直接存储对象,如果想要存储自定义对象,需要先转成NSData再存储。
首先在自定义的实体对象里继承NSCoding协议,再实现encodeWithCoder、initWithCoder方法即可
例如用户user表:user.h
#import <Foundation/Foundation.h> @interface User : NSObject <NSCoding> @property (strong, nonatomic) NSString *userId; @property (strong, nonatomic) NSString *headimg; @property (strong, nonatomic) NSString *createtime; @property (strong, nonatomic) NSString *integral; @property (strong, nonatomic) NSString *isadmin; @property (strong, nonatomic) NSString *mail; @property (strong, nonatomic) NSString *mobile; @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSString *qrcodeimg; @property (strong, nonatomic) NSString *sex; @property (strong, nonatomic) NSString *status; @property (strong, nonatomic) NSString *token; @property (strong, nonatomic) NSString *uuid; @property (strong, nonatomic) MyEstate *myEstateList; - (id)initWithCoder:(NSCoder *)aDecoder; - (void)encodeWithCoder:(NSCoder *)aCoder; + (User *)parser:(id)responseObject;
user.m
#import "User.h" #import <DCKeyValueObjectMapping.h> #import <DCParserConfiguration.h> #import <DCObjectMapping.h> @implementation User - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.userId = [aDecoder decodeObjectForKey:@"userId"]; self.headimg = [aDecoder decodeObjectForKey:@"headimg"]; self.createtime = [aDecoder decodeObjectForKey:@"createtime"]; self.integral = [aDecoder decodeObjectForKey:@"integral"]; self.isadmin = [aDecoder decodeObjectForKey:@"isadmin"]; self.mail = [aDecoder decodeObjectForKey:@"mail"]; self.mobile = [aDecoder decodeObjectForKey:@"mobile"]; self.name = [aDecoder decodeObjectForKey:@"name"]; self.qrcodeimg = [aDecoder decodeObjectForKey:@"qrcodeimg"]; self.sex = [aDecoder decodeObjectForKey:@"sex"]; self.status = [aDecoder decodeObjectForKey:@"status"]; self.token = [aDecoder decodeObjectForKey:@"token"]; self.uuid = [aDecoder decodeObjectForKey:@"uuid"]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.userId forKey:@"userId"]; [aCoder encodeObject:self.headimg forKey:@"headimg"]; [aCoder encodeObject:self.createtime forKey:@"createtime"]; [aCoder encodeObject:self.isadmin forKey:@"isadmin"]; [aCoder encodeObject:self.mail forKey:@"mail"]; [aCoder encodeObject:self.mobile forKey:@"mobile"]; [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeObject:self.qrcodeimg forKey:@"qrcodeimg"]; [aCoder encodeObject:self.sex forKey:@"sex"]; [aCoder encodeObject:self.status forKey:@"status"]; [aCoder encodeObject:self.token forKey:@"token"]; [aCoder encodeObject:self.uuid forKey:@"uuid"]; } + (User *)parser:(id)responseObject { DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass: [User class]]; return [parser parseDictionary:[responseObject objectForKey:@"data"]]; }
NSUserDefaults存储user
- (void)saveUser:(User *)user { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:user] forKey:DefaultUser]; }
NSUserDefaults查询user
- (User *)fetchUser { NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:DefaultUser]; if (userData) { User *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData]; return user; } return nil; }