标签:
iOS应用数据存储的常用方式
应用沙盒
模拟器应用沙盒的根路径在: (apple是用户名, 6.0是模拟器版本)/Users/apple/Library/Application Support/iPhone Simulator/6.0/Applications
应用沙盒结构分析
应用沙盒目录的常见获取方式
1.沙盒根目录:
NSString *home = NSHomeDirectory()
NSString *home = NSHomeDirectory(); NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; // 不建议采用,因为新版本的操作系统可能会修改目录名
2.利用NSSearchPathForDirectoriesInDomains函数
// NSUserDomainMask 代表从用户文件夹下找 // YES 代表展开路径中的波浪字符“~” NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素 NSString *documents = [array objectAtIndex:0];
3.tmp:目录
NSString *tmp = NSTemporaryDirectory();
4.Library/Caches目录(跟Documents类似的2种方法)
属性列表-归档NSDictionary
// 将数据封装成字典 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"母鸡" forKey:@"name"]; [dict setObject:@"15013141314" forKey:@"phone"]; [dict setObject:@"27" forKey:@"age"]; // 将字典持久化到Documents/stu.plist文件中 [dict writeToFile:path atomically:YES];
属性列表-恢复NSDictionary
// 读取Documents/stu.plist的内容,实例化NSDictionary NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; NSLog(@"name:%@", [dict objectForKey:@"name"]); NSLog(@"phone:%@", [dict objectForKey:@"phone"]); NSLog(@"age:%@", [dict objectForKey:@"age"]);
偏好设置
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"test" forKey:@"username"]; [defaults setFloat:18.0f forKey:@"text_size"]; [defaults setBool:YES forKey:@"auto_login"];
1.读取上次保存的设置
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *username = [defaults stringForKey:@"username"]; float textSize = [defaults floatForKey:@"text_size"]; BOOL autoLogin = [defaults boolForKey:@"auto_login"];
[defaults synchornize];
NSKeyedArchiver
encodeWithCoder: //每次归档对象时,都会调用这个方法。一般在这个方法里面指定如何归档对象中的每个实例变量,可以使用encodeObject:forKey:方法归档实例变量
initWithCoder: //每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用decodeObject:forKey方法解码实例变量
NSKeyedArchiver-归档NSArray
NSArray *array = [NSArray arrayWithObjects:@”a”,@”b”,nil];
[NSKeyedArchiver archiveRootObject:array toFile:path];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSKeyedArchiver-归档Person对象(Person.h)
//person.h
@interface Person : NSObject<NSCoding> @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) int age; @property (nonatomic, assign) float height; @end
//Pserson.m
@implementation Person - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeInt:self.age forKey:@"age"]; [encoder encodeFloat:self.height forKey:@"height"]; } - (id)initWithCoder:(NSCoder *)decoder { self.name = [decoder decodeObjectForKey:@"name"]; self.age = [decoder decodeIntForKey:@"age"]; self.height = [decoder decodeFloatForKey:@"height"]; return self; } - (void)dealloc { [super dealloc]; [_name release]; } @end
NSKeyedArchiver-归档Person对象(编码和解码)
Person *person = [[[Person alloc] init] autorelease]; person.name = @"MJ"; person.age = 27; person.height = 1.83f; [NSKeyedArchiver archiveRootObject:person toFile:path];
Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSKeyedArchiver-归档对象的注意
[super encodeWithCode:encode];
确保继承的实例变量也能被编码,即也能被归档。应该在initWithCoder:方法中加上一句
self = [super initWithCoder:decoder];
确保继承的实例变量也能被解码,即也能被恢复
NSData
create table t_person(name, age);
不过为了保证可读性,建议还是把字段类型加上:
create table t_person(name text, age integer);
char *errorMsg; // 用来存储错误信息 char *sql = "create table if not exists t_person(id integer primary key autoincrement, name text, age integer);"; int result = sqlite3_exec(db, sql, NULL, NULL, &errorMsg);
char *sql = "insert into t_person(name, age) values(?, ?);"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, "母鸡", -1, NULL); sqlite3_bind_int(stmt, 2, 27); } if (sqlite3_step(stmt) != SQLITE_DONE) { NSLog(@"插入数据错误"); } sqlite3_finalize(stmt);
char *sql = "select id,name,age from t_person;"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { int _id = sqlite3_column_int(stmt, 0); char *_name = (char *)sqlite3_column_text(stmt, 1); NSString *name = [NSString stringWithUTF8String:_name]; int _age = sqlite3_column_int(stmt, 2); NSLog(@"id=%i, name=%@, age=%i", _id, name, _age); } } sqlite3_finalize(stmt);
标签:
原文地址:http://www.cnblogs.com/JackieHoo/p/5058560.html