设为首页 加入收藏

TOP

ios应用数据存储方式(XML属性列表-plist) - 转(二)
2019-08-26 06:55:29 】 浏览:60
Tags:ios 应用 数据 存储 方式 XML 属性 -plist
N_WIDTH, BUTTON_HEIGHT)]; [_saveButton setTitle:
@"保存数据" forState:UIControlStateNormal]; [_saveButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [_saveButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_saveButton]; _readButton = [[UIButton alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2 - BUTTON_WIDTH/2, _saveButton.frame.origin.y + _saveButton.frame.size.height + 60, BUTTON_WIDTH, BUTTON_HEIGHT)]; [_readButton setTitle:@"读取数据" forState:UIControlStateNormal]; [_readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [_readButton addTarget:self action:@selector(readClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_readButton]; } - (void)saveClick{ //获取应用程序目录 NSString *home = NSHomeDirectory(); NSLog(@"应用程序目录:%@",home); //NSUserDomainMask在用户目录下查找 //YES 代表用户目录的~ //NSDocumentDirectory查找Documents文件夹 //建议使用如下方法动态获取 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"Documents文件夹路径:%@",doc); //拼接文件路径 NSString *path = [doc stringByAppendingString:@"/abc.plist"]; //NSArray *array = @[@"ios",@"23"]; //[array writeToFile:path atomically:YES]; //NSDictionary *dict = @{@"name":@"ios",@"age":@"28"}; //[dict writeToFile:path atomically:YES]; /* plist只能存储系统自带的一些常规的类,也就是有writeToFile方法的对象才可以使用plist保持数据 字符串/字典/数据/NSNumber/NSData ... */ //自定义的对象不能保存到plist中 DBPerson *person = [[DBPerson alloc] init]; person.name = @"ios"; } - (void)readClick{ NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [doc stringByAppendingString:@"/abc.plist"]; //读取数据 //NSArray *array = [NSArray arrayWithContentsOfFile:path]; //NSLog(@"%@",array); //NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; //NSLog(@"name = %@",[dict objectForKey:@"name"]); //NSLog(@"age = %@",[dict objectForKey:@"age"]); } @end

 

五.属性列表 
  1.属性列表是一种XML格式的文件,拓展名为plist。 
  2.如果对象是NSString,NSDictionary,NSArray,NSData,NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中 

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【读书笔记】iOS-更改编辑器键的.. 下一篇【读书笔记】iOS-使用GCD改善性能

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目