iPhone开发【二十一】数据持久化总结之第2篇—属性文件(.plist)
发布日期:2021-09-28 18:46:34 浏览次数:10 分类:技术文章

本文共 3730 字,大约阅读时间需要 12 分钟。

转载请注明出处,原文网址: 作者:张燕广

实现的功能:1)演示使用属性文件持久化数据。

关键词:数据持久化 属性文件 plist

1、新建一个Sigle View Application,命名为Persistence-file,工程结构如下

2、修改ViewController.xib,添加4个Label控件和4个TextField控件,如下:

3、修改ViewController.h,如下:

[cpp] 
  1. #define kFileName @"data.plist"  
  2. #import <UIKit/UIKit.h>  
  3.   
  4. @interface ViewController : UIViewController  
  5. @property(nonatomic,retain)IBOutlet UITextField *name;  
  6. @property(nonatomic,retain)IBOutlet UITextField *gender;  
  7. @property(nonatomic,retain)IBOutlet UITextField *age;  
  8. @property(nonatomic,retain)IBOutlet UITextField *education;  
  9.   
  10. -(NSString *)dataFilePath;  
  11. -(void)applicationWillResignActive:(NSNotification *)nofication;  
  12.   
  13. @end  
注意,需要连接各个输出口

4、修改ViewController.m,如下:

[cpp] 
  1. #import "ViewController.h"  
  2.   
  3. @implementation ViewController  
  4. @synthesize name,gender,age,education;  
  5.   
  6. #pragma mark - View lifecycle  
  7. - (void)viewDidLoad  
  8. {  
  9.     // Do any additional setup after loading the view, typically from a nib.  
  10.     [super viewDidLoad];  
  11.     UIApplication *app = [UIApplication sharedApplication];  
  12.     //订阅通知UIApplicationWillResignActiveNotification,进行数据保存操作  
  13.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];  
  14.     //初始化数据  
  15.     [self initData];  
  16. }  
  17.   
  18. -(void)initData{  
  19.     NSString *filePath = [self dataFilePath];  
  20.     NSLog(@"filePath=%@",filePath);  
  21.       
  22.     //从文件中读取数据,首先判断文件是否存在  
  23.     if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){  
  24.         NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];  
  25.         name.text = [array objectAtIndex:0];  
  26.         gender.text = [array objectAtIndex:1];  
  27.         age.text = [array objectAtIndex:2];  
  28.         education.text = [array objectAtIndex:3];  
  29.           
  30.         [array release];  
  31.     }  
  32. }  
  33.   
  34. -(void)applicationWillResignActive:(NSNotification *)nofication{  
  35.     NSMutableArray *array = [[NSMutableArray alloc]init];  
  36.     [array addObject:name.text];  
  37.     [array addObject:gender.text];  
  38.     [array addObject:age.text];  
  39.     [array addObject:education.text];  
  40.     //将数据写入到文件dataFilePath中  
  41.     [array writeToFile:[self dataFilePath] atomically:YES];  
  42.     [array release];  
  43. }  
  44.   
  45. //获得文件路径  
  46. -(NSString *)dataFilePath{  
  47.     //检索Documents目录  
  48.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);//备注1  
  49.     NSString *documentsDirectory = [paths objectAtIndex:0];//备注2  
  50.     return [documentsDirectory stringByAppendingPathComponent:kFileName];  
  51. }  
  52.   
  53. - (void)didReceiveMemoryWarning  
  54. {  
  55.     [super didReceiveMemoryWarning];  
  56.     // Release any cached data, images, etc that aren't in use.  
  57. }  
  58.   
  59. - (void)viewDidUnload  
  60. {  
  61.     [super viewDidUnload];  
  62.     // Release any retained subviews of the main view.  
  63.     // e.g. self.myOutlet = nil;  
  64.     self.name = nil;  
  65.     self.gender = nil;  
  66.     self.age = nil;  
  67.     self.education = nil;  
  68. }  
  69.   
  70. -(void)dealloc{  
  71.     [name release];  
  72.     [gender release];  
  73.     [age release];  
  74.     [education release];  
  75. }  
  76.   
  77. - (void)viewWillAppear:(BOOL)animated  
  78. {  
  79.     [super viewWillAppear:animated];  
  80. }  
  81.   
  82. - (void)viewDidAppear:(BOOL)animated  
  83. {  
  84.     [super viewDidAppear:animated];  
  85. }  
  86.   
  87. - (void)viewWillDisappear:(BOOL)animated  
  88. {  
  89.     [super viewWillDisappear:animated];  
  90. }  
  91.   
  92. - (void)viewDidDisappear:(BOOL)animated  
  93. {  
  94.     [super viewDidDisappear:animated];  
  95. }  
  96.   
  97. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  98. {  
  99.     // Return YES for supported orientations  
  100.     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);  
  101. }  
  102.   
  103. @end  
备注1、备注2:

NSDocumentDirectory:常量,表明正在查找Documents目录的路径

NSUserDomainMask:常量,表面将搜索限制与当前应用程序的沙盒中,这样的话只能找到一个Documents目录,因为每个应用程序沙盒中只有一个Documents目录,所以备注2中从paths中取第一个即为当前应用程序的Documents目录的路径

5、编译、运行,在TextField中输入内容,然后退出Simulator,进行测试:

6、通过上一篇对IOS应用程序沙盒的的介绍,本工程属性文件data.plist,保存的位置是:

/Users/duobianxing/Library/Application Support/iPhone Simulator/5.0/Applications/88528A36-00CB-4900-9762-1FD5D6AC8595/Documents

7、总结:

1)可以简单保存静态数据,但是无法将自定义对象序列化到属性列表中

2)该持久化方法与上一篇中介绍的NSUserDefaults实质上都是将数据写入到.plist属性文件中,区别是文件保存的位置不同。

需要源码的网友请留言哦

转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8568223 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iPhone开发【二十二】数据持久化总结之第3篇—归档(NSKeyedArchiver、NSKeyedUnarchiver)
下一篇:iPhone开发【二十】数据持久化总结之第1篇—NSUserDefaults

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月19日 15时07分17秒