标签:
数据存储的五种方法:
1、xml属性列表(plist归档)
2、NSKeyedArchiver归档(加密形式)
3、NSUserDefaults(偏好设置)
4、SQLite(嵌入式数据库)
5、Core Date(面向对象方式的嵌入式数据库)
一、plist的读写
缺点:
1、明文保存
2、操作对象有限只有NSArray、NSMutableArray、NSDictionary、NSMutableDictionary支持
(归档时只要调用对应的writeToFile方法即可,解档调用arrayWithContentsOfFile或dictionaryWithContentsOfFile)
写:
1、//获取到Document文件路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = paths[0];
2、//创建plist文件
NSString *filename = [path stringByAppendingPathComponent:@“test.plist”];
3、//建立Dictionary或者NSArray写入
NSDictionary *dic1 = @{@“name”:@“zhang”,@“age”:@22,@“sex”:@“man”};
NSDictionary *dic2 = @{@“name”:@“li”,@“age”:@54,@“sex”:@“female”};
NSDictionary *dic3 = @{@“name”:@“wang”,@“age”:@25,@“sex”:@“man”};
NSDictionary *dic = @{@“z”:dic1,@“l”:dic2,@“w”:dic3};
4、//写入到test.plist中
[dic writeToFile:filename atomically:YES];
读:
1、//获取到Document文件路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = paths[0];
2、//plist文件
NSString *filename = [path stringByAppendingPathComponent:@“test.plist”];
3、//读
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:filename];
NSDictionary *person = readDic[@“z”];
NSString *name = person[@“name”];
二、NSKeyedArchiver归档(加密形式)
分为简单归档(针对单个对象,不用设置key)和复杂对象归档(针对多个对象,需要设置不同的key)
OS X为:NSArchiver 和 NSUnarchiver
iOS 为:NSKeyedArchiver 和 NSKeyedUnarchiver
1、简单归档:
NSString *str = @“hello”;
//要归档的内容
//也可以是NSArray *array = @[@“a”,@“b”,@“c”];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = array[0];
NSString *filename = [path stringByAppendingPathComponent:@"test.arc"];
//获得路径
if(![NSKeyArchiver archiveRootObject:str toFile:filename])
{
NSLog(@“archiver failed!”);
}
//归档
(id) st = [NSKeyedUnarchiver unarchiverObjectWithFile:filename];
//解档
//(上面的id指任意的对象,可以是NSString,也可以是NSArray);
2、复杂对象归档(多对象归档)
//存档地址
NSString *filename2 = [path stringByAppendingPathComponent:@"test2.arc"];
归档:
//设置数据
int myInt = 31;
NSString *myString = @"hello”;
NSNumber *myNumber = @32;
NSArray *myArray = @[@"ok",@"no",@"byel"];
NSDictionary *myDic = @{@"dic1":@"a",@"dic2":@"b",@"dic3":@"c"};
//新建临时存放数据的data
NSMutableData *data = [[NSMutableData alloc]init];
//设置存档保存在data对象中
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//对对象进行归档,并设置key以便读取
[archiver encodeInt:myInt forKey:@"int"];
[archiver encodeObject:myString forKey:@"string"];
[archiver encodeObject:myNumber forKey:@"number"];
[archiver encodeObject:myArray forKey:@"array"];
[archiver encodeObject:myDic forKey:@"dictionary"];
//结束存档
[archiver finishEncoding];
//写入文件
[data writeToFile:filename2 atomically:YES];
读档:
//定义数据
int myInt2;
NSString *myString2;
NSNumber *myNumber2;
NSArray *myArray2;
NSDictionary *myDic2;
//获取到文件,存放在data2中
NSMutableData *data2 = [[NSMutableData alloc]initWithContentsOfFile:filename2];
//定义解档data2
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data2];
//解档并存放在变量中
myInt2 = [unarchiver decodeIntForKey:@"int"];
myString2 = [unarchiver decodeObjectForKey:@"string"];
myNumber2 = [unarchiver decodeObjectForKey:@"number"];
myArray2 = [unarchiver decodeObjectForKey:@"array"];
myDic2 = [unarchiver decodeObjectForKey:@"dictionary"];
//结束解档
[unarchiver finishDecoding];
三、NSUserDefaults
NSUserDefaults支持的数据类型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL.
1、存数据:
//将NSString 对象存储到 NSUserDefaults 中
NSString *passWord = @"1234567";
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:passWord forKey:@"userPassWord"];
2、取数据
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *passWord = [ user objectForKey:@"userPassWord"];
//存自定义的数据用NSData
四、SQLite数据库
特点:
1、在iOS中需要使用C语言语法进行数据库操作、访问(无法使用ObjC直接访问,因为libsqlite3框架基于C语言编写)
2、建立连接后通常不需要关闭连接(可以手动关闭)
步骤:(先导入libsqlite3框架)
1、打开数据库。
//用sqlite_open()打开数据库会指定一个数据库文件保存路径,如果文件存在则直接打开,否则创建并打开。(打开数据库会得到一个sqlite3类型的对象,后面需要借助这个对象进行其他操作)
2、执行SQL语句。
执行SQL语句包括有返回值的语句和无返回值语句。
(1)、对于有返回值的语句(如增删改等操作)直接通过sqlite3_exec()函数执行;
(2)、对于有返回值的语句(如查询操作)则首先通过sqlite3_prepare_v2()执行语法检测,然后通过sqlite3_step()依次取出查询结果的每一行数据,对于每行数据都可以通过对应的sqlite_column_类型名()方法获得对应列的数据,如此反复循环直到遍历完成。最后通过sqlite3_finalize()释放。
代码:
1、创建数据库
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:dbName];
2、打开数据库并判断是否打开
if (sqlite3_open([filename UTF8String], &database) == SQLITE_OK) {
NSLog(@"database open success");
}
else{
NSLog(@"database open failure");
}
3、处理一个没有返回值的sql语句(如增删改)
if (sqlite3_exec(database, [sql UTF8String], nil, nil, &error) != SQLITE_OK){
NSLog(@"%s",error);
}
4、处理一个有返回值的sql语句(如查询)
-(NSArray *)executeQuery:(NSString *)sql{
NSMutableArray *array = [[NSMutableArray alloc]init];
sqlite3_stmt *stmt;
int result = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
for (int i = 0; i < sqlite3_column_count(stmt); i ++) {
NSString *columnName =[NSString stringWithUTF8String:sqlite3_column_name(stmt, i)];
NSString *value=[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, i)];
dic[columnName] = value;
}
[array addObject:dic];
}
}
return array;
}
5、关闭数据库
if(sqlite3_close(database) != SQLITE_OK){
NSLog(@"%s",sqlite3_errmsg(database));
}
简单的数据存储——plist、NSKeyedArchiver归档、NSUserDefaults(偏好设置)、SQLite(嵌入式数据库)
标签:
原文地址:http://www.cnblogs.com/durwards/p/4558765.html