星空网 > 软件开发 > 操作系统

六:数据存储的五种方式(一)

  iOS开发中数据存在五种存储方式之三:

    1.plist(

    2.偏好设置

    3.NSKeydeArchiver归档(存储自定义对象)
 
一、plist(
第一种方式:(四个文件夹都可以取出路径)
1 //获取沙盒路径2   NSString *home = NSHomeDirectory();3   //获取documents的路径两种方式(其它三个文件夹也能获取)4   NSString *path = [home stringByAppendingString:@"/documents"];5   NSString *path1 = [home stringByAppendingPathComponent:@"library/cache"];

第二种方式:(仅有documents和cache能取出路径)(获取路径的时候必须写成YES,否则无法成功写入)

1 //获取cache路径,NO表示文件路径前面用“~”表示2 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject];3 NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];4 //拼接路径5 NSString *path = [cache stringByAppendingPathComponent:@"abc.plist"];6 NSString *path1 = [doucument stringByAppendingPathComponent:@"/ce.plist"];7 //把文件写入文件夹8 [arr writeToFile:path atomically:YES];9 [arr writeToFile:path1 atomically:YES];

读取文件:

1 //读取文件2 NSArray *arr1 = [NSArray arrayWithContentsOfFile:path];3 NSLog(@"%@",arr1);

 

二、偏好设置(保存用户名、密码、字体大小、是否自动登录等等基本设置)自动保存到沙盒的Preferences文件夹中

1 //获取对象2 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];3 //存储数据4 [ud setObject:@"abc" forKey:@"这是什么"];5 [ud setObject:@"cde" forKey:@"俺是"];6 //读取数据7 NSString *str = [ud objectForKey:@"俺是"];8 NSString *str1 = [ud objectForKey:@"这是什么"];9 NSLog(@"%@---%@",str,str1);

注意:偏好设置存储时候,保存的时间不确定,可能是将来的某个时间存储到文件夹中,所以可以马上同步存储:

    [ud synchronize];

 

三、NSKeyedArchive的归档(存储自定义对象)

在viewController.m中:

 1 - (void)viewDidLoad { 2   [super viewDidLoad]; 3    4   ZWPerson *p = [[ZWPerson alloc] init]; 5   p.name = @"mc"; 6   p.age = 18; 7   p.height = 1.88; 8   NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES) lastObject]; 9   NSString *doucument = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];10   //拼接路径11   NSString *path = [cache stringByAppendingPathComponent:@"abc.doc"];12   NSString *path1 = [doucument stringByAppendingPathComponent:@"/ce.doc"];13   //存储数据14   BOOL ar = [NSKeyedArchiver archiveRootObject:p toFile:path];15   BOOL ar1 = [NSKeyedArchiver archiveRootObject:p toFile:path1];16   NSLog(@"%d---%d",ar,ar1);17   //读取数据18   ZWPerson *p1 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];19   ZWPerson *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path1];20   NSLog(@"%zd---%@",p1.age,p2.name);21 }

新建ZWPerson类(继承自ViewController)

在ZWPerson.h中:

1 #import "ViewController.h"2 @interface ZWPerson : ViewController3 /** 名字 */4 @property (strong, nonatomic)NSString *name;5 /** 年龄 */6 @property (assign, nonatomic)NSInteger age;7 /** 身高 */8 @property (assign, nonatomic)double height;9 @end

在ZWPerson.h中:

 1 #import "ZWPerson.h" 2 @interface ZWPerson () 3 @end 4 @implementation ZWPerson 5 //存储的时候调用这个方法 6 - (void)encodeWithCoder:(NSCoder *)aCoder 7 { 8   //存储对象的属性(根据实际需要存储数据) 9   [aCoder encodeObject:self.name forKey:@"name"];10   [aCoder encodeInteger:self.age forKey:@"age"];11   [aCoder encodeDouble:self.height forKey:@"height"];12 }13 //读取的时候调用14 - (instancetype)initWithCoder:(NSCoder *)aDecoder15 {16   if (self = [super initWithCoder:aDecoder]) {.17     //获取对象的属性(根据实际需要获取数据)18     self.name = [aDecoder decodeObjectForKey:@"name"];19     self.age = [aDecoder decodeIntegerForKey:@"age"];20     self.height = [aDecoder decodeDoubleForKey:@"height"];21   }22   return self;23 }24 @end

如果是继承自NSObject:则只需要更改initWithCoder中的if(self = super init)即可

注意:1、遵守NSCoding协议,并实现该协议中的两个方法。

     2、如果是继承,则子类一定要重写那两个方法。因为person的子类在存取的时候,会去子类中去找调用的方法,没找到那么它就去父类中找,所以最后保存和读取的时候新增加的属性会被忽略。需要先调用父类的方法,先初始化父类的,再初始化子类的。

     3、保存数据的文件的后缀名可以随意命名。

     4、通过plist保存的数据是直接显示的,不安全。通过归档方法保存的数据在文件中打开是乱码的,更安全。





原标题:六:数据存储的五种方式(一)

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Jungle Scout有哪些功能?为什么这么多人用它呢?:https://www.ikjzd.com/articles/112232
增加亚马逊选品成功率有哪些好方法?:https://www.ikjzd.com/articles/112233
如何提升店铺转化率,运营独立站很重要。:https://www.ikjzd.com/articles/112234
黑五网一过后,别忘了做这些工作?:https://www.ikjzd.com/articles/112235
新规!亚马逊等电商企业每年提交FDI合规报告!:https://www.ikjzd.com/articles/112236
Wish不只是推送流量,还可以收割搜索流量:https://www.ikjzd.com/articles/112237
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
百崖大峡谷生态旅游景区(探秘中国西南自然风光):https://www.vstour.cn/a/363176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流