详解iPhone应用开发之数据持久化

移动开发 iOS
iPhone应用开发之数据持久化是本文要介绍的内容,主要是来学习iphone应用中数据库的使用,具体内容来看详细内容。

iPhone应用开发之数据持久化是本文要介绍的内容,主要是来学习iphone应用数据库的使用,具体内容来看详细内容。

1、plist

局限性:只有它支持的数据类型可以被序列化,存储到plist中。无法将其他Cocoa对象存储到plist,更不能将自定义对象存储。

支持的数据类型:Array,Dictionary,Boolean,Data,Date,Number和String.如图:

详解iPhone应用开发之数据持久化

xml文件 数据类型截图~其中基本数据(Boolean,Data,Date,Number和String.)、容器 (Array,Dictionary)

写入xml过程:先将基本数据写入容器 再调用容器的 writeToFile 方法,写入。

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

拥有此方法的数据类型有,如图所示:

详解iPhone应用开发之数据持久化

atomically参数,将值设置为 YES。写入文件的时候,将不会直接写入指定路径,而是将数据写入到一个“辅助文件”,写入成功后,再将其复制到指定路径。

2、Archiver

特点:支持复杂的数据对象。包括自定义对象。对自定义对象进行归档处理,对象中的属性需满足:为基本数据类型(int or float or......),或者为实现了NSCoding协议的类的实例。自定义对象的类也需要实现NSCoding。

NSCoding 方法:

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

参数分别理解为解码者和编码者。

例如创建自定义类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应用开发之数据持久化的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

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-07-27 10:16:41

iPhone SQLite 数据库

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-11 10:03:43

iPhonecocoaNSRunLoop

2011-08-15 11:37:20

iPhone开发Mask

2011-08-12 14:33:06

iPhone缓存文件

2011-07-27 11:14:37

iPhone UITableVie

2011-07-18 14:39:53

iPhone SDK UIKit

2011-08-11 17:15:54

iPhone归档

2011-08-09 11:36:41

iPhoneUIPickerVieDEMO

2011-08-12 10:04:24

iPhone开发视图

2011-07-26 09:41:23

iPhone xcode Mac OS X

2011-08-18 10:59:57

iPhone开发消息通信NSNotificat

2022-03-02 21:53:57

Spring数据库持久化Jar包

2011-08-16 15:36:47

iPhone应用测试

2011-08-17 15:10:21

iPhone开发Web视图

2011-08-15 17:52:21

iPhone应用对象NSString
点赞
收藏

51CTO技术栈公众号