自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解iPhone應(yīng)用開發(fā)之?dāng)?shù)據(jù)持久化

移動開發(fā) iOS
iPhone應(yīng)用開發(fā)之?dāng)?shù)據(jù)持久化是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iphone應(yīng)用中數(shù)據(jù)庫的使用,具體內(nèi)容來看詳細(xì)內(nèi)容。

iPhone應(yīng)用開發(fā)之數(shù)據(jù)持久化是本文要介紹的內(nèi)容,主要是來學(xué)習(xí)iphone應(yīng)用數(shù)據(jù)庫的使用,具體內(nèi)容來看詳細(xì)內(nèi)容。

1、plist

局限性:只有它支持的數(shù)據(jù)類型可以被序列化,存儲到plist中。無法將其他Cocoa對象存儲到plist,更不能將自定義對象存儲。

支持的數(shù)據(jù)類型:Array,Dictionary,Boolean,Data,Date,Number和String.如圖:

詳解iPhone應(yīng)用開發(fā)之?dāng)?shù)據(jù)持久化

xml文件 數(shù)據(jù)類型截圖~其中基本數(shù)據(jù)(Boolean,Data,Date,Number和String.)、容器 (Array,Dictionary)

寫入xml過程:先將基本數(shù)據(jù)寫入容器 再調(diào)用容器的 writeToFile 方法,寫入。

  1. [theArray writeToFile:filePath atomically:YES]; 

擁有此方法的數(shù)據(jù)類型有,如圖所示:

詳解iPhone應(yīng)用開發(fā)之?dāng)?shù)據(jù)持久化

atomically參數(shù),將值設(shè)置為 YES。寫入文件的時候,將不會直接寫入指定路徑,而是將數(shù)據(jù)寫入到一個“輔助文件”,寫入成功后,再將其復(fù)制到指定路徑。

2、Archiver

特點:支持復(fù)雜的數(shù)據(jù)對象。包括自定義對象。對自定義對象進(jìn)行歸檔處理,對象中的屬性需滿足:為基本數(shù)據(jù)類型(int or float or......),或者為實現(xiàn)了NSCoding協(xié)議的類的實例。自定義對象的類也需要實現(xiàn)NSCoding。

NSCoding 方法:

  1. -(id)initWithCoder:(NSCoder *)decoder; - (void)encodeWithCoder:(NSCoder *)encoder;  

參數(shù)分別理解為解碼者和編碼者。

例如創(chuàng)建自定義類Student:NSObject <NSCoding>

  1. #import "Student.h"   
  2. @implementation Student   
  3. @synthesize studentID;   
  4. @synthesize studentName;   
  5. @synthesize age;   
  6. @synthesize count;   
  7. - (void)encodeWithCoder:(NSCoder *)encoder  
  8. {  
  9.    [encoder encodeObject: studentID forKey: kStudentId];  
  10.     [encoder encodeObject: studentName forKey: kStudentName];  
  11.     [encoder encodeObject: age forKey: kAge];  
  12.      [encoder encodeInt:count forKey:kCount];   
  13. }18 19  - (id)initWithCoder:(NSCoder *)decoder  
  14. {  
  15.      if (self == [super init]) {  
  16.        self.studentID = [decoder decodeObjectForKey:kStudentId];  
  17.        self.studentName = [decoder decodeObjectForKey:kStudentName];          
  18.    self.age = [decoder decodeObjectForKey:kAge];  
  19.         self.count = [decoder decodeIntForKey:kCount];   
  20.     }  
  21.     return self;  
  22. }  
  23. @end 

編碼過程:

  1. /*encoding*/   
  2.  Student *theStudent = [[Student alloc] init];  
  3.  theStudent.studentID = @"神馬";   
  4.  theStudent.studentName = @"shenma";  
  5.  theStudent.age = @"12";   
  6.   NSMutableData *data = [[NSMutableData alloc] init];   
  7.  NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];   
  8.  [archiver encodeObject: theStudent forKey:@"student"]; 

NSKeyedArchiver可以看作“加密器”,將student實例編碼后存儲到data

NSMutableData 可看作“容器”,并由它來完成寫入文件操作(inherits NSData)。

解碼過程:

  1.  /*unencoding*/  
  2. Student *studento = [[Student alloc] init];  
  3. data = [[NSData alloc] initWithContentsOfFile:documentsPath];  
  4.  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  5.  studento = [unarchiver decodeObjectForKey:@"student"];  
  6.  [unarchiver finishDecoding]; 

根據(jù)鍵值key得到反序列化后的實例。

3、SQLite

數(shù)據(jù)庫操作~

小結(jié):詳解iPhone應(yīng)用開發(fā)之數(shù)據(jù)持久化的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-07 17:16:47

iPhone 數(shù)據(jù)

2011-07-07 15:45:45

iPhone SQLite 數(shù)據(jù)

2021-03-18 08:18:15

ZooKeeper數(shù)據(jù)持久化

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-07-27 10:16:41

iPhone SQLite 數(shù)據(jù)庫

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2024-09-29 09:25:53

2011-07-18 14:39:53

iPhone SDK UIKit

2011-08-12 14:33:06

iPhone緩存文件

2011-07-27 11:14:37

iPhone UITableVie

2011-08-15 11:37:20

iPhone開發(fā)Mask

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-08-11 17:15:54

iPhone歸檔

2011-08-18 10:59:57

iPhone開發(fā)消息通信NSNotificat

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-08-12 10:04:24

iPhone開發(fā)視圖

2022-03-02 21:53:57

Spring數(shù)據(jù)庫持久化Jar包

2011-08-16 15:36:47

iPhone應(yīng)用測試

2011-08-17 15:10:21

iPhone開發(fā)Web視圖
點贊
收藏

51CTO技術(shù)棧公眾號