当 AppDelegate 中的 didFinishLaunchingWithOptions 时,我将 plist 复制到文档目录,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self performSelector:@selector(copyPlist)];
return YES;
}
- (void)copyPlist {
NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"Obj.plist"];
if ([fileManger fileExistsAtPath:destinationPath]){
NSLog(@"database localtion %@",destinationPath);
return;
}
NSString *sourcePath=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"Obj.plist"];
[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
}
然后我试图在我的视图中显示 plist 的内容,如下所示:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Obj.plist"];
NSLog(@"plist path %@", path);
sortedObj = [[NSMutableArray alloc]initWithContentsOfFile:path];
NSLog(@"objects %@", sortedObj);
记录结果:
数据库位置:
/Users/kdb/Library/Application Support/iPhone Simulator/5.1/Applications/4E165740-01CD-4ED3-8971-FDCCB1751DD1/Documents/Obj.plist
plist路径:
与数据库位置相同
对象:
(空值)
内容为空。如何让它发挥作用?
plist 是一个带有字典的数组。