【问题标题】:Plist NSFileManager copyItemAtPath:toPath:error:] source path is nil'Plist NSFileManager copyItemAtPath:toPath:error:] 源路径为 nil'
【发布时间】:2013-01-13 05:47:27
【问题描述】:

尝试创建 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];
}

【问题讨论】:

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


    【解决方案1】:

    - [NSBundle pathForResource:ofType:] 如果请求的文件不存在,则返回nil。确保 app bundle 确实包含 data.plist

    【讨论】:

    • -pathForResource:ofType:进行递归搜索,因此请确保您指向包含您的 plist 的正确目录。
    • 这段代码不应该创建 Plist 文件吗?你能引导我到“App Bundle”吗?
    • @Ofir_Tel.Aviv 这两个句子都没有意义/没有意义。我不明白你的问题是什么。
    • 问题是当应用程序午餐时,它崩溃了,并出现上述错误消息.. 因为 plist 捆绑错误,但代码似乎没问题
    • 应用字典没有出现在路径上!库 -> 应用程序支持 -> iPhone 模拟器 -> 5.1(和任何模拟器) -> 应用程序 -> [app 文件夹] -> 文档
    猜你喜欢
    • 2012-06-15
    • 2011-05-18
    • 2011-06-29
    • 2011-01-07
    • 2015-07-13
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多