NSString
属性列表
把数据存储到沙盒中再从沙盒中取出,明确沙盒目录有两种方式
一种
// 获取应用沙盒的根路径
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
二种
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
读取或写入沙盒documents中
#pragma mark 将字典写入属性列表文件中
- (void)writeDict {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"mj" forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:10] forKey:@"age"];
// 获取应用沙盒的根路径
NSString *home = NSHomeDirectory();
NSLog(@"%@",home);
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
// 属性列表的默认拓展名是plist
NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];
[dict writeToFile:path atomically:YES];
}
#pragma mark 从属性列表文件中读取字典
- (void)readDict {
// 获取应用沙盒的根路径
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
// 属性列表的默认拓展名是plist
NSString *path = [documents stringByAppendingPathComponent:@"dict.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"%@", dict);
}
自定义对象进行归档是编码与解码需要实现NSCoding encodewithCoder initWithCoder
#pragma mark 读取多个Person
- (void)readPersons {
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documents stringByAppendingPathComponent:@"persons.plist" ];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@", array);
}
#pragma mark 写入多个Person
- (void)writePersons {
NSMutableArray *array = [NSMutableArray array];
for (int i =0; i<3 ; i++) {
NSString *name = [NSString stringWithFormat:@"mj-%i", i];
int age = 10 + i;
Person *p = [Person personWithName:name age:age];
[array addObject:p];
}
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documents stringByAppendingPathComponent:@"persons.plist" ];
}
偏好设置 Preferences
一般用于写入系统中的文档数据,比如用户注册,修改皮肤颜色,修改字体。。
#pragma mark 读取偏好设置
- (void)read {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *useranme = [defaults objectForKey:@"username"];
NSString *password = [defaults objectForKey:@"password"];
BOOL autoLogin = [defaults boolForKey:@"auto_login"];
NSLog(@"%@-%@-%i",useranme, password, autoLogin);
}
#pragma mark 存储偏好设置
- (void)save {
// NSUserDefaults关联着沙盒中的Preferences文件夹
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"mj" forKey:@"username"];
[defaults setObject:@"123" forKey:@"password"];
[defaults setBool:YES forKey:@"auto_login"];
// 将数据同步到Preferences文件夹中
[defaults synchronize];
}
Sqlite3
是开源的小型数据库,嵌入式关系型数据库,可移植性好,已使用内存开销小,无类型,数据类型,Text Integer,Float ,boolean ,blob
1打开数据库
// 数据库文件路径
NSString *filename = [@"sqlite.db" documentsAppend];
// 如果数据库不存在,就会创建一个
int result = sqlite3_open([filename UTF8String], &_db);
if (result == SQLITE_OK) {
2,创建表,
返回结果 数据库 语句 错误
int result = sqlite3_exec(_db, sql, NULL, NULL, &error);
3插入数据
// sqlite3_prepare_v2做一些插入数据的准备
// 主要是检查SQL语句的语法问题
int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
// sqlite3_prepare_v2做一些插入数据的准备
// 主要是检查SQL语句的语法问题
int result = sqlite3_prepare_v2(_db, sql, -1, &stmt, NULL);
// 关闭数据库
sqlite3_close(_db);
Core Data
是苹果公司自带的存储
资源这比较详细可观看 http://www.cnblogs.com/mjios/archive/2013/02/26/2932999.html