IOS Data Storage--Archive

Source: Internet
Author: User

Four methods of data persistence http://www.cocoachina.com/ios/20160617/16739.html

First, the data in the model of the solution file

。 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

Save

Model Data-"archive-" Nsuserdefaults

Nsuserdefaults * defaults = [Nsuserdefaults standarduserdefaults];

cannot access the NSObject directly, it needs to be archived and turned into NSData

NSData *data = [Nskeyedarchiver archiveddatawithrootobject:model];

[Defaults setobject:data forkey:@ "model"];

nsuserdefaults--"Model data "

NSData *modeldata = [[nsuserdefaults standarduserdefaults]objectforkey:@ "model"];

Usermodel = [Nskeyedunarchiver unarchiveobjectwithdata:modeldata];

Save in file

#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 Data Storage--Archive

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.