iOS 数据持久性存储--属性列表存储

简介:

iOS上常用四种数据存取方法有:

属性列表

对象归档

iOS的嵌入式关系数据库(SQLite3)

苹果公司提供持久性共聚Core Data


它们的应用程序都有自己的/Documents文件夹,各自的应用程序只能读写自己的/Documents目录内容


1.创建一个新工程叫PersistenceDemo; File->New->Project ->single View Application -> next


2.在PersistenceViewController.h文件声明输出口和和实例方法

#import <UIKit/UIKit.h>  @interface PersistenceViewController : UIViewController  @property (weak, nonatomic) IBOutlet UITextField *field1; @property (weak, nonatomic) IBOutlet UITextField *field2; @property (weak, nonatomic) IBOutlet UITextField *field3; @property (weak, nonatomic) IBOutlet UITextField *field4;  -(NSString *)dataFilePath; -(void)applicationWillResignActive:(NSNotification *)notification;  @end

3.打开 PersistenceViewController.xib文件拖4个laibel静态标签和4个TextField,然后和输出口关联(右键Fiel‘s Owner 拖到TextField上松开)

 

4.在对应的.m文件中

dataFilePath将kFileName串联到Documents目录的路径,以创建并返回数据文件的完整路径名

-(NSString *)dataFilePath {     /*常量NSDocumentDirectory表明我们正在查找Documents目录路径,第二个常量NSUserDomainMask表示的是把搜索范围定在应用程序沙盒中,YES表示的是希望希望该函数能查看用户主目录*/          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //  数组索引0处Documentd目录,       NSString *documentDirectory = [paths objectAtIndex:0]; //    返回一个kFileName的完整路径     return [documentDirectory stringByAppendingPathComponent:kFileName]; }


- (void)viewDidLoad {     [super viewDidLoad]; //  检查数据文件是否存在,不存在则不加载       NSString *filePath = [self dataFilePath];     if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {         NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];                  field1.text = [array objectAtIndex:0];         field2.text = [array objectAtIndex:1];         field3.text = [array objectAtIndex:2];         field4.text = [array objectAtIndex:3];     } //    注册一个通知,按下home键,执行applicationWillResignActive:方法     UIApplication *app = [UIApplication sharedApplication];     [[NSNotificationCenter defaultCenter]addObserver:self                                             selector:@selector(applicationWillResignActive:)                                                 name:UIApplicationWillTerminateNotification                                               object:app];      	 }


所有通知都接受一个NSNotification的参数
//当按下home键的时候调用这个方法 -(void)applicationWillResignActive:(NSNotification *)notification {     NSMutableArray *array = [[NSMutableArray alloc] init];     [array addObject:field1.text];     [array addObject:field2.text];     [array addObject:field3.text];     [array addObject:field4.text]; //    将TextField中数据写入到属性表之中     [array writeToFile:[self dataFilePath] atomically:YES]; }

5. NSNotification 和 NSNotificationCenter

Notification对象非常简单. 它就是poster要提供给observer的信息包裹. notification对象有两个重要的成员变量: name 和 object. 一般object都是指向poster(为了让observer在接受到notification时可以回调到poster)
所以,notification有两个方法
    - (NSString *)name
    - (id)object
NSNotificaitonCernter是架构的大脑了.它允许我们注册observer对象, 发送notification, 撤销observer对象注册
下面是它的一些常用方法
+ (NSNotificationCenter *)defaultCenter
返回notification center [类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]

- (void)addObserver:(id)anObserver
           selector:(SEL)aSelector
               name:(NSString *)notificationName
             object:(id)anObject

注册anObserver对象:接受名字为notificationName, 发送者为anObject的notification. 当anObject发送名字为notificationName的notification时, 将会调用anObserver的aSelector方法,参数为该notification. 如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer

- (void)postNotification:(NSNotification *)notification
发送notification至notification center
- (void)postNotificationName:(NSString *)aName
                      object:(id)anObject

创建并发送一个notification
- (void)removeObserver:(id)observer
移除observer

引用 http://blog.csdn.net/zhanglei5415/article/details/6454940




源代码:http://download.csdn.net/detail/duxinfeng2010/4445711



     本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208732,如需转载请自行联系原作者






相关文章
|
3月前
|
存储 iOS开发 开发者
使用克魔助手进行iOS数据抓包和HTTP抓包的方法详解
使用克魔助手进行iOS数据抓包和HTTP抓包的方法详解
47 0
|
3月前
|
Web App开发 网络安全 Android开发
🚀2023最新版克魔助手抓包教程(9) - 克魔助手 IOS 数据抓包
在移动应用程序的开发中,了解应用程序的网络通信是至关重要的。数据抓包是一种很好的方法,可以让我们分析应用程序的网络请求和响应,了解应用程序的网络操作情况。克魔助手是一款非常强大的抓包工具,可以帮助我们在 Android 和 iOS 平台上进行数据抓包。本篇博客将介绍如何使用克魔助手在 iOS 平台上进行数据抓包。
|
缓存 iOS开发
iOS小技能:解决TableVIew刷新数据带来的界面跳动问题
iOS小技能:解决TableVIew刷新数据带来的界面跳动问题
1504 0
iOS小技能:解决TableVIew刷新数据带来的界面跳动问题
|
存储 iOS开发
iOS小技能: get 和post 布尔值参数处理、按照时间分页的数据重复的处理
1. get 和post 布尔值参数处理:如果后台Bool 参数没有同时支持【 0,1】 ;和【 true false】,get请求的时候就需要特殊处理。 2. 按照时间分页的数据重复的处理
144 0
iOS小技能: get 和post 布尔值参数处理、按照时间分页的数据重复的处理
|
存储 Web App开发 JSON
iOS小技能:设备ID除了使用_idfa、_idfv 还可使用其他替代方案(Keychain 存储)
设备信息的获取:除了使用_idfa、_idfv, 还使用sysct 获取cpu信息。
269 0
iOS小技能:设备ID除了使用_idfa、_idfv 还可使用其他替代方案(Keychain 存储)