IOS常用的三种本地数据持久化方式:
1:属性序列化列表 存储到文件 plist;
2:归档序列化,对模型数据进行归档;
3:本地数据库存储,嵌入式sqlite;
其实这三种方式都是把数据存储到本地文件里边,只是实现方式和使用的场景不同而已,复杂程度从上到下增加。
除此之外,还有一种方式存储数据,就是使用NSUserDefaults,它已键值对的形式记录应用程序的全局设置,其实settings.bundle里边的数据就是存在NSUserDefaults的。
主要代码如下:
方式一:plist
//先获取plist文件路径 NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [path objectAtIndex:0]; filePath = [documentDirectory stringByAppendingPathComponent:@"xxx.plist"]; //把数据写入plist NSArray *arr = [[NSArray allocl] initWithObjects:A,B,C,nil]; [arr writeToFile:self.filePath atomically:YES]; //从plist取数据 NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
方式二:archiver
//创建继承NSCoding,NSCopying协议的数据模型User,必须实现以下三个方法: @synthesize passport; @synthesize password; @synthesize nickname; @synthesize remark; - (void)encodeWithCoder:(NSCoder *)encoder{ [encoder encodeObject:passport forKey:kPassportKey]; [encoder encodeObject:password forKey:kPasswordKey]; [encoder encodeObject:nickname forKey:kNicknameKey]; [encoder encodeObject:remark forKey:kRemarkKey]; } - (id)initWithCoder:(NSCoder *)decoder{ if (self=[super init]) { self.passport = [decoder decodeObjectForKey:kPassportKey]; self.password = [decoder decodeObjectForKey:kPasswordKey]; self.nickname = [decoder decodeObjectForKey:kNicknameKey]; self.remark = [decoder decodeObjectForKey:kRemarkKey]; } return self; } - (id)copyWithZone:(NSZone *)zone{ User *copy = [[[self class] allocWithZone:zone] init]; passport = [self.passport copy]; password = [self.password copy]; nickname = [self.nickname copy]; remark = [self.remark copy]; return copy; } //获取归档文件 - (NSString *)dataFilePath{ NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [(NSString *)[arr objectAtIndex:0] stringByAppendingPathComponent:@"archiver"]; } //初始化数据模型并写入归档文件 User *user = [[User alloc] init]; user.passport = @"alec030711"; user.password = @"abcd"; user.nickname = @"alecchyi"; user.remark = @"fuck"; NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:user forKey:@"kData"]; [archiver finishEncoding]; [data writeToFile:[self dataFilePath] atomically:YES]; [archiver release]; [data release]; [user release]; //从归档文件读数据 NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; User *user = [unArchiver decodeObjectForKey:@"kData"]; [unArchiver finishDecoding]; [data release]; [unArchiver release];
方式三:sqlite
sqlite3 *database; //打开数据库,没有则创建 if (sqlite3_open([数据库文件路径 UTF8String], &database) != SQLITE_OK) { sqlite3_close(database); } //创建表 char *errorStr; NSString *createTable = @"drop table if exists users;create table if not exists users (id integer primary key,nickname text);"; if (sqlite3_exec(database,[createTable UTF8String],NULL,NULL,&errorStr)!=SQLITE_OK) { sqlite3_close(database); } //插入数据 for (int x=1; x<5; x++) { NSString *insert = [[NSString alloc] initWithFormat:@"insert or replace into users (id,nickname) values (%d,'ruby%d')",x,x]; if (sqlite3_exec(database,[insert UTF8String],NULL,NULL,&errorStr)!=SQLITE_OK) { sqlite3_free(database); } [insert release]; } //查询表数据 NSString *query = @"select nickname,id from users"; sqlite3_stmt *statememt; if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statememt,nil)==SQLITE_OK) { while (sqlite3_step(statememt)==SQLITE_ROW) { char *rowData = (char *)sqlite3_column_text(statememt,0); NSString *nickname = [[NSString alloc] initWithUTF8String:rowData]; [nickname release]; } } sqlite3_finalize(statememt); //关闭数据库 sqlite3_close(database); //关键就这几个方法和几个语句:sqlite3_open, sqlite3_exec,sqlite3_prepare_v2 sqlite3_step sqlite_finalize,sqlite3_close。
方式三需要引入libsqlite3.lib,由于sqlite数据库是C实现的,存储也是C形式存储,所以在取出数据时需要用UTF8String转换成objecitve-c形式的字符串