偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

詳解iPhone應用開發(fā)之數據持久化

移動開發(fā) iOS
iPhone應用開發(fā)之數據持久化是本文要介紹的內容,主要是來學習iphone應用中數據庫的使用,具體內容來看詳細內容。

iPhone應用開發(fā)之數據持久化是本文要介紹的內容,主要是來學習iphone應用數據庫的使用,具體內容來看詳細內容。

1、plist

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

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

詳解iPhone應用開發(fā)之數據持久化

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

寫入xml過程:先將基本數據寫入容器 再調用容器的 writeToFile 方法,寫入。

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

擁有此方法的數據類型有,如圖所示:

詳解iPhone應用開發(fā)之數據持久化

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

2、Archiver

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

NSCoding 方法:

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

參數分別理解為解碼者和編碼者。

例如創(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]; 

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

3、SQLite

數據庫操作~

小結:詳解iPhone應用開發(fā)之數據持久化的內容介紹完了,希望通過本文的學習能對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網
相關推薦

2011-06-07 17:16:47

iPhone 數據

2011-07-07 15:45:45

iPhone SQLite 數據

2021-03-18 08:18:15

ZooKeeper數據持久化

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-07-27 10:16:41

iPhone SQLite 數據庫

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-07-18 14:39:53

iPhone SDK UIKit

2011-07-27 11:14:37

iPhone UITableVie

2011-08-12 14:33:06

iPhone緩存文件

2011-08-15 11:37:20

iPhone開發(fā)Mask

2024-09-29 09:25:53

2011-08-11 17:15:54

iPhone歸檔

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

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ā)視圖

2011-08-16 15:36:47

iPhone應用測試

2022-03-02 21:53:57

Spring數據庫持久化Jar包

2011-08-17 15:10:21

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

51CTO技術棧公眾號