在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"];
}