在iOS開發(fā)中我們常常需要做數(shù)據(jù)存儲(chǔ)春锋,這里主要說的時(shí)以下幾種本地?cái)?shù)據(jù)存儲(chǔ)的方式。
NSUserDefaults
屬性列表: 集合對(duì)象可以讀寫到屬性列表中
對(duì)象歸檔:對(duì)象可以保存到歸檔文件中
SQLite數(shù)據(jù)庫:開源嵌入式關(guān)系型數(shù)據(jù)庫
FMDB:
NSUserDefaults
NSUserDefaults適合存儲(chǔ)輕量級(jí)的本地?cái)?shù)據(jù)唇聘,比如用戶的信息,登錄狀態(tài)等數(shù)據(jù)。
NSUserDefaults支持的數(shù)據(jù)格式有:NSNumber(Integer慎菲、Float、Double)锨并,NSString露该,NSDate,NSArray第煮,NSDictionary解幼,BOOL類型
// 存
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"iOS開發(fā)者" forKey:@"name"];
[defaults synchronize];
// 取
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSString *name = [userDefault objectForKey:@"name"];
屬性列表
屬性列表文件是一種XML文件,F(xiàn)oundation框架中的數(shù)組和字典等都可以于屬性列表文件相互轉(zhuǎn)換包警。
- NSArray類常用讀寫屬性列表文件的方法:
+arrayWithContentsOfFile:類級(jí)構(gòu)造方法撵摆,用于從屬性列表文件中讀取數(shù)據(jù),創(chuàng)建NSArray對(duì)象害晦。
-initWithContentsOfFile:實(shí)例構(gòu)造方法特铝,用于從屬性列表文件中讀取數(shù)據(jù),創(chuàng)建NSArray對(duì)象壹瘟。
-writeToFile:atomically:該方法把NSArray對(duì)象寫入到屬性列表文件中鲫剿,第一個(gè)參數(shù)是文件名,第二個(gè)參數(shù)為是否使用輔助文件稻轨,如果為YES灵莲,則先寫入到一個(gè)輔助文件,然后輔助文件再重新命名為目標(biāo)文件殴俱,如果為NO政冻,則直接寫入到目標(biāo)文件。
// 存儲(chǔ)路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"array.plist"];
// 寫數(shù)據(jù)
NSArray *array = @[@"test1", @"test2", @"test3"];
[array writeToFile:path atomically:YES];
// 讀數(shù)據(jù)
NSArray *getArray = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"---讀取的數(shù)據(jù)---%@", getArray);
- NSDictionary類常用讀寫屬性列表文件的方法:
+dictionaryWithContentsOfFile:類級(jí)構(gòu)造方法线欲,用于從屬性列表文件中讀取數(shù)據(jù)赠幕,創(chuàng)建NSDictionary對(duì)象。
-initWithContentsOfFile:實(shí)例構(gòu)造方法询筏,用于從屬性列表文件中讀取數(shù)據(jù)榕堰,創(chuàng)建NSDictionary對(duì)象。
-writeToFile:atomically:該方法將NSDictionary對(duì)象寫入到屬性列表文件中。
// 存儲(chǔ)路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"dict.plist"];
// 寫數(shù)據(jù)
NSDictionary *dict = @{@"a":@"test1", @"b":@"test2", @"c":@"test3"};
[dict writeToFile:path atomically:YES];
// 讀數(shù)據(jù)
NSDictionary *getDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"---讀取的數(shù)據(jù)---%@", getDict);
對(duì)象歸檔
對(duì)象歸檔是一種序列化方式逆屡。存儲(chǔ)即將歸檔對(duì)象序列化位一個(gè)文件進(jìn)行持久化圾旨,讀取是通過反歸檔將持久化的數(shù)據(jù)恢復(fù)到對(duì)象中。
注意:可歸檔的對(duì)象必須實(shí)現(xiàn)NSCoding協(xié)議
- 對(duì) Foundation 庫中對(duì)象進(jìn)行歸檔(這里不再介紹)
- 自定義對(duì)象進(jìn)行歸檔 (需要實(shí)現(xiàn)歸檔協(xié)議魏蔗、NSCoding)
自定義類實(shí)現(xiàn)NSCoding協(xié)議
@interface Person : NSObject <NSCoding>
@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *sex;
@property(nonatomic, assign) NSInteger age;
@end
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (!(self = [super init])) {
return nil;
}
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
return self;
}
@end
歸檔/解檔
Person *person = [[Person alloc] init];
person.name = @"iOS開發(fā)著";
person.sex = @"男";
person.age = 1000;
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"person.data"];
// 歸檔
[NSKeyedArchiver archiveRootObject:person toFile:file];
// 解檔
Person *per = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
if (per) {
NSLog(@"-------解歸檔---name----: %@", per.name);
NSLog(@"-------解歸檔---sex----: %@", per.sex);
NSLog(@"-------解歸檔---age----: %ld", per.age);
}