iOS数据存取的常见方式:
- XML属性列表(plist)归档,通常的路径是Documents
对象是NSDictionary、NSArray,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中
存
将数据封装成字典
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"小红" forKey:@"name"];
[dic setObject:@"15013141314" forKey:@"phone"];
[dic setObject:@"27" forKey:@"age"];
// 将字典持久化到Documents/stu.plist文件中
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"];
[dic writeToFile:filepath atomically:YES];
// 或者是数组
// 注意:如果为YES则保证文件的写入原子性,就是说会先创建一个临时文件,直到文件内容写入成功再导入到目标文件里.
// 如果为NO,则直接写入目标文件里.
读
读取Documents/stu.plist的内容,实例化NSDictionary或者NSArray
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"
];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"name:%@", [dict objectForKey:@"name"]);
NSLog(@"phone:%@", [dict objectForKey:@"phone"]);
NSLog(@"age:%@", [dict objectForKey:@"age"]);
// 如果写入的是数组,就用下面的方法取值
NSArray *array= [NSArray arrayWithContentsOfFile:filepath];
- preference(偏好设置): 直接写到Library/Preferences:目录下面;保存用户名、字体大小、是否自动登录.
存
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"itcast" forKey:@"username"];
[defaults setFloat:18.0f forKey:@"text_size"];
[defaults setBool:YES forKey:@"auto_login"];
// 注意:UserDefaults设置数据时,不是立即写入,而是根据时间戳定时地把缓存中的数据写入本地磁盘。所以调用了set方法之后数据有可能还没有写入磁盘应用程序就终止了,因此要调用synchronize方法强制写入
[defaults synchronize];
读
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *username = [defaults stringForKey:@"username"];
float textSize = [defaults floatForKey:@"text_size"];
BOOL autoLogin = [defaults boolForKey:@"auto_login"];
- NSKeyedArchiver归档(NSCoding):可以存储对象
(1) NSString、NSDictionary、NSArray、NSData、NSNumber等类型,可以直接用NSKeyedArchiver进行归档和恢复
(2)不是所有的对象都可以直接用这种方法进行归档,只有遵守了<NSCoding>协议的对象才可以;
(3).NSCoding协议有2个方法:
一是:
encodeWithCoder:每次归档对象时,都会调用这个方法,一般在这个方法里面指定如何归档对象中的每个实例变量,encodeObject:forKey:方法归档实例变量;
二是:
initWithCoder:每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用decodeObject:forKey方法解码实例变量;
// 首先要对对象实行下面方法:
- (void)encodeWithCoder:(NSCoder *)encoder{
[encoder encodeObject:self.no forKey:@"no"];
[encoder encodeInt:self.age forKey:@"age"];
[encoder encodeDouble:self.height forKey:@"height"];
}
- (id)initWithCoder:(NSCoder *)decoder
{ if (self = [super init]) {
// 读取文件的内容
self.no = [decoder decodeObjectForKey:@"no"];
self.age = [decoder decodeIntForKey:@"age"];
self.height = [decoder decodeDoubleForKey:@"height"];
}
return self;
}
存
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 获得文件的全路径
NSString *path = [doc stringByAppendingPathComponent:@"xxx.data"];
//将对象归档
[NSKeyedArchiver archiveRootObject:stu toFile:path];
读
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"xxx.data"];
Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
存取对象数组
存
NSMutableArray *itemsDataArray = [NSMutableArray array];
for (RWTItem *item in self.items) {
NSData *itemData = [NSKeyedArchiver archivedDataWithRootObject:item];
[itemsDataArray addObject:itemData];
}
[[NSUserDefaults standardUserDefaults] setObject:itemsDataArray forKey:kRWTStoredItemsKey];
[[NSUserDefaults standardUserDefaults] synchronize];
读
NSArray *storedItems = [[NSUserDefaults standardUserDefaults] arrayForKey:kRWTStoredItemsKey];
self.items = [NSMutableArray array];
if (storedItems) {
for (NSData *itemData in storedItems) {
RWTItem *item = [NSKeyedUnarchiver unarchiveObjectWithData:itemData];
[self.items addObject:item];
}
}
网友评论