NSUserDefaultsカスタムオブジェクトの格納
9900 ワード
NSUserDefaultsストレージタイプが限られているため(NSNumber(NSInteger、float、double)、NSString、NSDate、NSArray、NSDictionary、BOOL.)、ダイレクトストレージオブジェクトはサポートされていません.カスタムオブジェクトを保存するには、NSDataに移行してからストレージする必要があります.
まずカスタムエンティティオブジェクトにNSCodingプロトコルを継承し,encodeWithCoder,initWithCoderメソッドを実装すればよい.
例えばユーザuserテーブル:user.h
user.m
NSUserDefaultsストレージuser
NSUserDefaultsクエリーuser
転載先:https://www.cnblogs.com/MasterPeng/p/7515954.html
まずカスタムエンティティオブジェクトにNSCodingプロトコルを継承し,encodeWithCoder,initWithCoderメソッドを実装すればよい.
例えばユーザuserテーブル:user.h
#import
@interface User : NSObject
@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
#import
#import
@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;
}
転載先:https://www.cnblogs.com/MasterPeng/p/7515954.html