沙盒根目录:NSString *home = NSHomeDirectory(); Documents:(2种方式) 1. 利用沙盒根目录拼接”Documents”字符串 //不建议采用,因为新版本的操作系统可能会修改目录名 NSString *home = NSHomeDirectory(); NSString *documents = [home stringByAppendingPathComponent:@"Documents"]; 2. 利用NSSearchPathForDirectoriesInDomains函数 // NSUserDomainMask 代表从用户文件夹下找 // YES 代表展开路径中的波浪字符“~” NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO); // 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素 NSString *documents = [array objectAtIndex:0]; tmp: NSString *tmp = NSTemporaryDirectory(); Library/Caches: 跟Documents类似的2种方法: 1. 利用沙盒根目录拼接”Caches”字符串 2. 利用NSSearchPathForDirectoriesInDomains函数(将函数的第2个参数改为:NSCachesDirectory即可) Library/Preference: 通过NSUserDefaults类存取该目录下的设置信息
/ /获取路径 NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; path = [path stringByAppendingPathComponent:@"save.plist"]; / /将数据封装成字典 //不能存储对象 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"小黄" forKey:@"name"]; [dict setObject:@"12345678" forKey:@"phone"]; [dict setObject:@"24" forKey:@"age"]; // 将字典持久化到Documents/save.plist文件中 [dict writeToFile:path atomically:YES];
读取属性列表,恢复NSDictionary对象 // 读取Documents/save.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:@"mr.zk" forKey:@"username"]; [defaults setFloat:18.0f forKey:@"text_size"]; [defaults setBool:YES forKey:@"auto_login"];
//读取上次保存的设置 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *username = [defaults stringForKey:@"username"]; float textSize = [defaults floatForKey:@"text_size"]; BOOL autoLogin = [defaults boolForKey:@"auto_login"];
NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; path = [path stringByAppendingPathComponent:@"array.data"]; NSArray *array = [NSArray arrayWithObjects:@”a”,@”b”,nil]; [NSKeyedArchiver archiveRootObject:array toFile:path];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSKeyedArchiver-归档Person对象(Person.h) @interface Person : NSObject<NSCoding> @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) int age; @property (nonatomic, assign) float height; @end
@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] autorelease]; person.name = @"zk"; person.age = 24; person.height = 1.73f; [NSKeyedArchiver archiveRootObject:person toFile:path]; //恢复(解码) Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
//归档(编码) // 新建一块可变数据区 NSMutableData *data = [NSMutableData data]; // 将数据区连接到一个NSKeyedArchiver对象 NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease]; // 开始存档对象,存档的数据都会存储到NSMutableData中 [archiver encodeObject:person1 forKey:@"person1"]; [archiver encodeObject:person2 forKey:@"person2"]; // 存档完毕(一定要调用这个方法) [archiver finishEncoding]; // 将存档的数据写入文件 [data writeToFile:path atomically:YES];
//恢复(解码) // 从文件中读取数据 NSData *data = [NSData dataWithContentsOfFile:path]; // 根据数据,解析成一个NSKeyedUnarchiver对象 NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; Person *person1 = [unarchiver decodeObjectForKey:@"person1"]; Person *person2 = [unarchiver decodeObjectForKey:@"person2"]; // 恢复完毕 [unarchiver finishDecoding];
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有