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(@“myfile‘s 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] myfile’s path is :/Users/sundfsun2009/Library/Application
Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf
Terminating in response to SpringBoard‘s 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] myfile’s path is :/Users/sundfsun2009/Library/Application
Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf
Terminating in response to SpringBoard‘s termination.
从打印的结果如下,每次应用程序启动时生成的数字字母串目录名字并不一样。在调用[NSKeyedArchiver
archiveRootObject:persistentArray toFile:myFilename]方法前,文件myFile.rtf并每生成,只有在调用此方法后才产生相应的
文件。
在所有的移动开发平台数据持久化都是很重要的部分:在j2me中是rms或保存在应用程序的目录中,在symbian中可以保
存在相应的磁盘目录中和数据库中。symbian中因为权限认证的原因,在3rd上大多数只能访问应用程序的private目录或其它
系统共享目录。在iphone中,apple博采众长,提供了多种数据持久化的方法,下面笔者会逐个进行详细的讲解。
评论0
最新资源