偏好设置:
应用程序包:(上图中的Layer)包含了所有的资源文件和可执行文件 Documents:保存应用运行时生成的需要持久化的数据,iTunes同步设备时会备份该目录。例如,游戏应用可将游戏存档保存在该目录 tmp:保存应用运行时所需的临时数据,使用完毕后再将相应的文件从该目录删除。应用没有运行时,系统也可能会清除该目录下的文件。iTunes同步设备时不会备份该目录 Library/Caches:保存应用运行时生成的需要持久化的数据,iTunes同步设备时不会备份该目录。一般存储体积大、不需要备份的非重要数据 Library/Preference:保存应用的所有偏好设置,iOS的Settings(设置)应用会在该目录中查找应用的设置信息。iTunes同步设备时会备份该目录
plist文件详解:
#import "ViewController.h" #import "Contact.h" @interface ViewController () @property (nonatomic,copy) NSString *plistPath; @end @implementation ViewController -(NSString *)plistPath{ if (!_plistPath) { // 获取document目录 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"%@",doc); _plistPath = [doc stringByAppendingPathComponent:@"data.plist"]; } return _plistPath; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //查看沙盒的路径 //1.控制台打印沙盒的路径,使用finder-前往-文件夹 打开 //2.控制台打印沙盒的路径,打开终端 open + 路径 //3.使用simpholders工具 //4.可以断点 输入po NSHomeDirectory() //获取缓存路径(cache) NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"%@",cachePath); //获取临时路径(tmp) NSString *tmpPath = NSTemporaryDirectory(); NSLog(@"%@",tmpPath); //主目录 NSString *homePath = NSHomeDirectory(); NSLog(@"%@",homePath); } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self testString]; //不是所有对象都有可使用write方法 Contact *contact = [[Contact alloc] init]; contact.name = @"zhangsan"; } -(void)testString{ NSString *data = @"aabc"; //写字符串 [data writeToFile:self.plistPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; //读字符串 NSString *str = [NSString stringWithContentsOfFile:self.plistPath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",str); } -(void)testDictionary{ //把数据保存到沙盒的一个plist文件里 NSDictionary *data = @{@"name":@"zhangsan",@"age":@23}; //把数据写入一个文件 [data writeToFile:self.plistPath atomically:YES]; //读取 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:self.plistPath]; NSLog(@"%@",dict); } /** * 测试数组存储在一个plist文件 */ -(void)testArray{ //把数据保存到沙盒的一个plist文件里 NSArray *data = @[@"asd",@"asds"]; //把数据写入一个文件 [data writeToFile:self.plistPath atomically:YES]; //读取 NSArray *array = [NSArray arrayWithContentsOfFile:self.plistPath]; NSLog(@"%@",array); }
用户偏好设置详解:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self modify]; } -(void)modify{ //修改 用户偏好设置 数据 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:@"321" forKey:@"password"]; [defaults synchronize]; } -(void)delete{ //删除 用户偏好设置 数据 NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults]; [defautls removeObjectForKey:@"account"]; //删除记得也要调用 同步的方法 [defautls synchronize]; } -(void)read{ //读取 用户偏好设置 的数据 NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults]; NSString *account = [defautls objectForKey:@"account"]; NSString *password = [defautls objectForKey:@"password"]; BOOL rememberPwd = [defautls boolForKey:@"rememberPwd"]; BOOL autoLogin = [defautls boolForKey:@"autoLogin"]; NSLog(@"%@ %@ %d %d",account,password,rememberPwd,autoLogin); } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //使用偏好设置保存数据 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //保存的数据 [defaults setObject:@"zhangsan" forKey:@"account"]; [defaults setObject:@"123" forKey:@"password"]; [defaults setBool:YES forKey:@"autoLogin"]; [defaults setBool:YES forKey:@"rememberPwd"]; //调用同步的方法,把数据保存到沙盒文件 [defaults synchronize]; }
时间: 2024-10-13 11:35:34