天天看点

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

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

属性列表

对象归档

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

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

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

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

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

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

4.在对应的.m文件中

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

所有通知都接受一个NSNotification的参数

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

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