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,如需轉載請自行聯系原作者