iOS 資料儲存--歸檔

來源:互聯網
上載者:User

標籤: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 資料儲存--歸檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.