1. 程式人生 > >iOS如何儲存使用者資訊之plist檔案/歸檔

iOS如何儲存使用者資訊之plist檔案/歸檔

在iOS開發過程中註冊登入往往是必不可少的, 那麼如何儲存使用者的這些註冊資訊在本地呢?

  • 示例

這裡寫圖片描述

這裡我建立了一個名為LoginController的控制器, 有五個NSString屬性, 分別是str0...str4, 和一個TextField的陣列; 同時還有四個按鈕, 作用分別是儲存資訊到plist檔案, 載入pilst檔案儲存的資訊, 歸檔資訊, 載入歸檔檔案裡的資訊.

LoginController.h程式碼:

@interface LoginController : UIViewController<NSCoding>
@property (nonatomic
) NSArray<UITextField *> *tfArray; @property (nonatomic, copy) NSString *str0; @property (nonatomic, copy) NSString *str1; @property (nonatomic, copy) NSString *str2; @property (nonatomic, copy) NSString *str3; @property (nonatomic, copy) NSString *str4; @end

注意必須要遵守NSCoding, 否則無法實現歸檔和解檔

儲存方式一: plist檔案儲存

LoginController.m程式碼:

  • 儲存按鈕點選事件
 //儲存資料
- (void)saveBtnClick {
    NSMutableArray *dataArr = [NSMutableArray new];
    for (int i = 0; i < self.tfArray.count; i++) {
        [dataArr addObject:self.tfArray[i].text];
    }
    NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/person.plist"
]; [dataArr writeToFile:str atomically:YES]; NSLog(@"使用者資訊儲存路徑: %@", str); }
  • 載入plist檔案儲存的資訊的按鈕點選事件
- (void)loadBtnClick {
    NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/person.plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:str];
    for (int i = 0; i < self.tfArray.count; i++) {
        self.tfArray[i].placeholder = arr[i];
    }
}

儲存方式二: 歸檔

  • 歸檔按鈕點選事件
- (void)archiveBtnClick {
    LoginController *per = [[LoginController alloc] init];
    per.str0 = self.tfArray[0].text;
    per.str1 = self.tfArray[1].text;
    per.str2 = self.tfArray[2].text;
    per.str3 = self.tfArray[3].text;
    per.str4 = self.tfArray[4].text;
    NSLog(@"per------%@%@%@", per.str0, per.str1, per.str3);
    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archive = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //歸檔資料
    [archive encodeObject:per forKey:@"me"];
    //結束歸檔
    [archive finishEncoding];
    //將歸檔資料寫入磁碟
    NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/guidang.zhengmengxin"];
    [data writeToFile:str atomically:YES];
    NSLog(@"歸檔:%@", str);
}
  • 解檔按鈕載入解檔內容事件
- (void)loadArchiveBtnClick {
    NSString *str = [NSHomeDirectory() stringByAppendingString:@"/Documents/guidang.zhengmengxin"];
    NSData *data = [NSData dataWithContentsOfFile:str];
    NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    //解檔資料
    LoginController *vc = [unarchive decodeObjectForKey:@"me"];
    [unarchive finishDecoding];
    NSLog(@"vc------%@%@%@", vc.str0, vc.str1, vc.str3);
    self.tfArray[0].text = vc.str0;
    self.tfArray[1].text = vc.str1;
    self.tfArray[2].text = vc.str2;
    self.tfArray[3].text = vc.str3;
    self.tfArray[4].text = vc.str4;
}

必須實現的NSCoding協議方法

我們先來看看NSCoding協議的API

@protocol NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER

@end

LoginController.m中實現協議的兩個方法:

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.str0 = [aDecoder decodeObjectForKey:@"str0"];
        self.str1 = [aDecoder decodeObjectForKey:@"str1"];
        self.str2 = [aDecoder decodeObjectForKey:@"str2"];
        self.str3 = [aDecoder decodeObjectForKey:@"str3"];
        self.str4 = [aDecoder decodeObjectForKey:@"str4"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.str0 forKey:@"str0"];
    [aCoder encodeObject:self.str1 forKey:@"str1"];
    [aCoder encodeObject:self.str2 forKey:@"str2"];
    [aCoder encodeObject:self.str3 forKey:@"str3"];
    [aCoder encodeObject:self.str4 forKey:@"str4"];
}