Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录
tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录
Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据
Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录
1.plist存储:
写入:
NSString *home = NSHomeDirectory();
NSString *path = [home stringByAppendingPathComponent:@"Documents"];
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"]
;
NSArray *arr = @[@"lnj", @"28"];
[arr writeToFile:path atomically:YES];
读取:
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [doc stringByAppendingPathComponent:@"abc.plist"]
;
NSArray *array=[NSArray arrayWithContentsOfFile:path];
特点:
plist只能存储系统自带的一些常规的类, 也就是有writeToFile方法的对象才可以使用plist保存数据:
字符串/字典/数据/NSNumber/NSData ...
自定义的对象不能保存到plist中
2.偏好设置存储:
存储:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"lnj" forKey:@"name"];
[defaults setObject:@"man" forKey:@"gender"];
[defaults setInteger:13 forKey:@"age"];
[defaults setDouble:10.1 forKey:@"height"];
[defaults synchronize];
读取:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults objectForKey:@"name"];
NSInteger age = [defaults integerForKey:@"age"];
特点:本质就是plist存储.
3.归档:
在自定义对象中实现NSCoding协议并实现以下两个方法:
- (void)encodeWithCoder:(NSCoder *)encoder
{
NSLog(@"NJPerson encodeWithCoder");
[encoder encodeObject:self.name forKey:@"name"];
[encoder encodeInteger:self.age forKey:@"age"];
[encoder encodeFloat:self.height forKey:@"heigth"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
NSLog(@"NJPerson initWithCoder");
if (self = [super init]) {
self.name = [decoder decodeObjectForKey:@"name"];
self.age = [decoder decodeIntegerForKey:@"age"];
self.height = [decoder decodeFloatForKey:@"heigth"];
}
return self;
}
存储:
NJPerson *p = [[NJPerson alloc] init];
p.name = @"lnj";
p.age = 28;
p.height = 1.76;
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"stu.arc"];
[NSKeyedArchiver archiveRootObject:p toFile:path];
读取:
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [docPath stringByAppendingPathComponent:@"stu.arc"];
NJPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
注意点:若是有NJStudent继承NJPerson,需要这样做:
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[super encodeWithCoder:aCoder];
NSLog(@"NJStudent encodeWithCoder");
[aCoder encodeFloat:self.weight forKey:@"weight"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"NJStudent initWithCoder");
self.weight = [aDecoder decodeFloatForKey:@"weight"];
}
return self;
}
特点:实现自定义对象的归档