Plist NSFileManager copyItemAtPath:toPath:error:] source path is nil'

时间:2013-01-29 15:38:52

标签: ios objective-c plist nsfilemanager info.plist

尝试创建plist文件。 但是获取错误 [NSFileManager copyItemAtPath:toPath:error:]:源路径为

有人知道为什么会这样吗? 非常感谢..

AppDelegate.h

NSError*error;
NSArray*pathes=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString*documentDirectory=[pathes objectAtIndex:0];//first dir in app dir.
NSString*path=[documentDirectory stringByAppendingPathComponent:@"data.plist"];

NSFileManager*filemanager=[NSFileManager defaultManager];
if(![filemanager fileExistsAtPath:path])
{
    NSString*bundle=[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];//check if plist file exist;



    [filemanager copyItemAtPath:bundle toPath:path error:&error];
    NSMutableDictionary*data=[[NSMutableDictionary alloc]initWithContentsOfFile:path];
    NSArray*arr=[[NSArray alloc]init];
    [data setObject:arr forKey:@"arr"];
    [data writeToFile:path atomically:YES];
}

1 个答案:

答案 0 :(得分:0)

如果请求的文件不存在,

- [NSBundle pathForResource:ofType:]将返回nil。确保应用包真的包含data.plist