IOS 沙盒两种数据读取方式&NSUserDefaults读取方式

沙盒数据读取写入

NSString *doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];


NSString *path=[doc stringByAppendingPathComponent:@"account.archive”];

1.获取沙盒路径

2.如果是数组字典的话

[responseObject writeToFile:path atomically:YES];

NSDictionary *account=[NSDictionary dictionaryWithContentsOfFile:path];

3.如果是自定义Model的话

[NSKeyedArchiver archiveRootObject:account toFile:path];

AccountModel *account1=[NSKeyedUnarchiver unarchiveObjectWithFile:path];


NSUserDefaults

NSString *key=@"CFBundleVersion";

写入:

[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];

 // 这个不可省 立即执行


 [[NSUserDefaults standardUserDefaults] synchronize];

读取:

NSString *lastVersion=[[NSUserDefaults standardUserDefaults] objectForKey:key];



 // info.plist中获取版本号


NSString *currentVersion=[NSBundle mainBundle].infoDictionary[key];

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-09-15 12:19  MrYude  阅读(277)  评论(0编辑  收藏  举报

导航