保存并获取数据

在IOS开发过程中,经常会遇到数据保存的问题,现在主要说一下用write方法保存数据,并在需要使用时获取数据,使用write to file方法可以使数据永久的保存在磁盘中,具体方法:

NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSLog(@"path = %@",path);


NSString *FileName=[documentDirectory stringByAppendingPathComponent:@"test.plist"];//test.plist就是保存文件的文件名


NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:label2.text,@"1",label4.text,@"2",label6.text,@"3", label7.text,@"4", nil];//其中label.text是想保存的value值,并在后边获取
[dic writeToFile:filename atomically:YES];


NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];//获取数据所在的路径
NSLog(@" %@",path);
NSString *file=[path stringByAppendingPathComponent:@"test.plist"];//获取数据的文件
dic2 = [NSDictionary dictionaryWithContentsOfFile:file];//将获取的数据定义到另一个字典中
NSLog(@" %@",dic2);

这是在不同的controller之间获取数据,若是在同一个controller获取数据则不需要获取路径直接用self.path就行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值