码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 数据储存--归档

时间:2017-05-20 14:28:26      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:span   ref   target   mic   port   代码   fork   nis   ble   

四种数据持久化方式 http://www.cocoachina.com/ios/20160617/16739.html 

//先把数据在model中归解档

。h

技术分享
@property(nonatomic,copy)NSString *email;
@property(nonatomic,copy)NSString *uid;
@property(nonatomic,copy)NSString *mobile;
@property(nonatomic,copy)NSString *vip;
@property(nonatomic,copy)NSString *regtime;
@property(nonatomic,copy)NSString *is_sec_pwd;
技术分享

 

*is_sec_pwd;

。m

技术分享
#import "UserModel.h"
static UserModel *model;
@implementation UserModel
+(UserModel *)defaultCellModel{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!model) {
            model = [[UserModel alloc]init];
        }
    });
    return model;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.account forKey:@"account"];
    [aCoder encodeObject:self.regmobile forKey:@"regmobile"];
    [aCoder encodeObject:self.sec1_question forKey:@"sec1_question"];
    [aCoder encodeObject:self.headimage forKey:@"headimage"];
    [aCoder encodeObject:self.expert_audit forKey:@"expert_audit"];
  
    
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    
    if(self)
        
    {
        
        self.account = [aDecoder decodeObjectForKey:@"account"];
        
        self.regmobile = [aDecoder decodeObjectForKey:@"regmobile"];
        self.sec1_question = [aDecoder decodeObjectForKey:@"sec1_question"];
        self.headimage = [aDecoder decodeObjectForKey:@"headimage"];
   
        
    }
    
    return self;

}

@end
技术分享

 

//

NSUserDefaults

 保存

Model数据-》归档-》NSUserdefaults 

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

//不能直接存取NSObject,需要先归档转成NSData

NSData  *data  = [NSKeyedArchiver archivedDataWithRootObject:model];             

 [defaults setObject:data forKey:@"model"];

NSUserdefaults-》解档-》Model数据

NSData *modeldata = [[NSUserDefaults standardUserDefaults]objectForKey:@"model"];

 usermodel = [NSKeyedUnarchiver unarchiveObjectWithData:modeldata];

 

 

 

//文件中保存

#import "ViewController.h"
#import "linePesist.h"
//导入数据模型类
#define CodeString  @"CodeString"
@implementation ViewController
-(NSString *)dataFile{
NSArray *ar = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *fielpath = [ar objectAtIndex:0];
return [fielpath stringByAppendingPathComponent:@"data.archive"];
//改修后缀名,以免与属性列表创建的文件重复,而加载成旧的的文件。 不用查字典了。。archive表归档
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filepath = [self dataFile];
NSLog(@"%@",filepath);
if([[NSFileManager defaultManager]fileExistsAtPath:filepath]) {
//创建2个实例
NSData *data = [[NSData alloc]initWithContentsOfFile:filepath]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//把已归档的对象读取。赋值给linepesist
linePesist *linepesist =[unarchiver decodeObjectForKey:CodeString]; [unarchiver finishDecoding];
//完成解档
for(int i = 0;i

iOS 数据储存--归档

标签:span   ref   target   mic   port   代码   fork   nis   ble   

原文地址:http://www.cnblogs.com/xujiahui/p/6882009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!