深入理解iPhone数据持久化(手把手教你iphone开发 - 基础篇)
作者:孙东风 2009-11-11(请尊重作者劳动成果,转载务必注明出处)
在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保存在相应的磁盘目录中和数据库中。symbian中因为权限认证的原因,在3rd上大多数只能访问应用程序的private目录或其它系统共享目录。在iphone中,apple博采众长,提供了多种数据持久化的方法,下面笔者会逐个进行详细的讲解。
iphone提供的数据持久化的方法,从数据保存的方式上讲可以分为三大部分:属性列表、对象归档、嵌入式数据库(SQLite3)、其他方法。
一、属性列表NSUserDefaults
NSUserDefaults类的使用和NSKeyedArchiver有很多类似之处,但是查看NSUserDefaults的定义可以看出,NSUserDefaults直接继承自NSObject而NSKeyedArchiver 继承自NSCoder。这意味着NSKeyedArchiver实际上是个归档持久化的类,也就可以使用NSCoder类的[encodeObject: (id)objv forKey:(NSString *)key]方法来对数据进行持久化存储。
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSMutableArray *persistentArray = [[NSMutableArray alloc] init];
[persistentArray addObject:strOne];
[persistentArray addObject:strTwo];
//archive
NSUserDefaults *persistentDefaults = [NSUserDefaults standardUserDefaults];
[persistentDefaults setObject:persistentArray forKey:@"myDefault"];
NSString *descriptionDefault = [persistentDefaults description];
NSLog(@"NSUserDefaults description is :%@",descriptionDefault);
//unarchive
NSArray *UnpersistentArray =
[persistentDefaults objectForKey:@"myDefault"];
NSString *UnstrOne = [UnpersistentArray objectAtIndex:0];
NSString *UnstrTwo = [UnpersistentArray objectAtIndex:1];
NSLog(@"UnstrOne = %@,UnstrTwo = %@",UnstrOne,UnstrTwo);
// Override point for customization after application launch
[window makeKeyAndVisible];
}
二、对象归档NSKeyedArchiver和NSKeyedUnarchiver
iPhone和symbian 3rd一样,会为每一个应用程序生成一个私有目录,这个目录位于
/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并随即生成一个数字字母串作为目录名,在每一次应用程序启动时,这个字母数字串都是不同于上一次的,上一次的应用程序目录信息被转换成名为.DS_Store隐藏文件,这个目录的文件结构如下图:
通常使用Documents目录进行数据持久化的保存,而这个Documents目录可以通过NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)得到,代码如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSArray *persistentArray = [NSArray arrayWithObjects:strOne,strTwo,nil];
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
int pathLen = [pathArray count];
NSLog(@"path number is :%d",pathLen);
NSString *filePath;
for(int i = 0; i < pathLen; i++)
{
filePath = [pathArray objectAtIndex:i];
NSLog(@"%d path is :%@",i,filePath);
}
NSString *myFilename = [filePath stringByAppendingPathComponent:@"myFile.rtf"];
NSLog(@"myfiles path is :%@",myFilename);
// no files generated in correspond directory now
[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename];
// now the myFile.rtf is generated
// Override point for customization after application launch
[window makeKeyAndVisible];
}
NSSearchPathForDirectoriesInDomains()的第二个参数是个枚举值,在笔者的测试代码中,只有NSUserDomainMask和NSAllDomainsMask可以获取到目录数为1,其余的皆为0,打印出来的结果如下:
[Session started at 2009-11-10 21:30:08 +0800.]
2009-11-10 21:30:10.516 PersistentExample[763:207] path number is :1
2009-11-10 21:30:10.518 PersistentExample[763:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents
2009-11-10 21:30:10.521 PersistentExample[763:207] myfiles path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf
Terminating in response to SpringBoards termination.
[Session started at 2009-11-10 21:32:27 +0800.]
2009-11-10 21:32:30.091 PersistentExample[803:207] path number is :1
2009-11-10 21:32:30.092 PersistentExample[803:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents
2009-11-10 21:32:30.100 PersistentExample[803:207] myfiles path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf
Terminating in response to SpringBoards termination.
从打印的结果如下,每次应用程序启动时生成的数字字母串目录名字并不一样。在调用[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename]方法前,文件myFile.rtf并每生成,只有在调用此方法后才产生相应的文件。
下面需要把数据从属性列表中读取出来,在上面的代码中,笔者使用NSArray保存数据。但在大多数应用程序中,数据的尺寸并不是固定的,这个时候就需要使用NSMutalbeArray动态的保存数据,代码优化如下:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSString *myFilename;
// archive
{
NSString *strOne = @"Persistent data1";
NSString *strTwo = @"Persistent data 2";
NSMutableArray *persistentArray = [[NSMutableArray alloc] init];
[persistentArray addObject:strOne];
[persistentArray addObject:strTwo];
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);
int pathLen = [pathArray count];
NSLog(@"path number is :%d",pathLen);
NSString *filePath;
for(int i = 0; i < pathLen; i++)
{
filePath = [pathArray objectAtIndex:i];
NSLog(@"%d path is :%@",i,filePath);
}
myFilename = [filePath stringByAppendingPathComponent:@"myFile.rtf"];
NSLog(@"myfiles path is :%@",myFilename);
[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename];
}
// unarchive
{
NSArray *unarchiveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:myFilename];
NSString *UnstrOne = [unarchiveArray objectAtIndex:0];
NSString *UnstrTwo = [unarchiveArray objectAtIndex:1];
NSLog(@"UnstrOne = %@,UnstrTwo = %@",UnstrOne,UnstrTwo);
}
// Override point for customization after application launch
[window makeKeyAndVisible];
}
输出结果如下:
[Session started at 2009-11-10 22:41:57 +0800.]
2009-11-10 22:41:59.344 PersistentExample[1082:207] path number is :1
2009-11-10 22:41:59.346 PersistentExample[1082:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents
2009-11-10 22:41:59.355 PersistentExample[1082:207] myfiles path is :/Users/sundfsu
补充:移动开发 , IOS ,