1.iOS应用数据存储的常用方式
2.应用沙盒
》位置:/Users/用户名/Library/Developer/CoreSimulator/Devices
如果不知道在哪里,可以在项目中打出来看一上:NSLog(@"%@",NSHomeDirectory());
》应用沙盒目录的常见获取方式
NSString *home = NSHomeDirectory();
NSString *documents = [home stringByAppendingPathComponent:@"Documents"];
// 不建议采用,因为新版本的操作系统可能会修改目录名
// NSUserDomainMask 代表从用户文件夹下找
// YES 代表展开路径中的波浪字符“~”
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, NO);
// 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素
NSString *documents = [arrayobjectAtIndex:0];
>tmp:NSString*tmp= NSTemporaryDirectory();》存入Documents文件夹中:
获取沙盒路径:NSString * home = NSHomeDirectory();
document路径:NSString *docPath = [home stringByAppendingPathComponent:@“Documents”];
存数据:filePath:具体的文件名(xxx.plist)(非文件夹)路径,再用类对象方法:writeToFile: atomiclly: 写入数据
NSString * filePath = [docPath stringByAppendingPathComponent@“xxx.plist”];
// 将数据封装成字典
NSMutableDictionary *dict =[NSMutableDictionary dictionary];
[dict setObject:@"母鸡" forKey:@"name"];
[dict setObject:@"15013141314" forKey:@"phone"];
[dict setObject:@"27" forKey:@"age"];
// 将字典持久化到Documents/stu.plist文件中
[dict writeToFile:filePath atomically:YES];
取出: 读取属性列表,恢复NSDictionary对象// 读取Documents/stu.plist的内容,实例化NSDictionary
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"name:%@", [dict objectForKey:@"name"]);
NSLog(@"phone:%@", [dict objectForKey:@"phone"]);
NSLog(@"age:%@", [dict objectForKey:@"age"]);
》存入偏好设置(Library/Preferences):
1.利用NSUserDefaults,就能直接访问软件的偏好设置
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
// 存储数据(就像字典存数据)
[defaults setFloat:18.0f forKey:@"text_size"];
[defaults setBool:YES forKey:@"auto_login"];
[defaults synchronize];
NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];
NSString *username = [defaults stringForKey:@"username"];
float textSize = [defaults floatForKey:@"text_size"];
BOOL autoLogin = [defaults boolForKey:@"auto_login"];
[defaults synchornize];
》<NSCoding>, NSKeyedArchiver, NSKeyedUnarchiver
每次归档对象时,都会调用这个方法。一般在这个方法里面指定如何归档对象中的每个实例变量,可以使用encodeObject:forKey:方法归档实例变量
每次从文件中恢复(解码)对象时,都会调用这个方法。一般在这个方法里面指定如何解码文件中的数据为对象的实例变量,可以使用decodeObject:forKey方法解码实例变量
>例1:NSKeyedArchiver-归档NSArray
NSArray*array = [NSArray arrayWithObjects:@”a”,@”b”,nil];
[NSKeyedArchiver archiveRootObject:arraytoFile:path];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
>例2:NSKeyedArchiver-归档Person对象
// Person.h头文件
@interface Person : NSObject<NSCoding>
@property(nonatomic,copy) NSString*name;
@property(nonatomic,assign) intage;
@property(nonatomic,assign) float height;
@end
// Person.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;
}
@end
Person*person = [[Person alloc] init] ;
person.name = @"xxx";
person.age = 27;
person.height = 1.83f;
[NSKeyedArchiver archiveRootObject:person toFile:filepath];
Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
[super encodeWithCode:encode];
确保继承的实例变量也能被编码,即也能被归档
self= [super initWithCoder:decoder];
确保继承的实例变量也能被解码,即也能被恢复