【问题标题】:Why does the copyItemAtPath:toPath:userPath failed to copy the file when the file really exists? NSFileNoSuchFileError, Cocoa Error Code 4当文件确实存在时,为什么 copyItemAtPath:toPath:userPath 无法复制文件? NSFileNoSuchFileError,可可错误代码 4
【发布时间】:2012-10-10 15:32:46
【问题描述】:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
}


NSString *dbFilename = @"CoordinatesDatabase.sql";
NSString *userPath = [[CoordinatesAppDelegate applicationDocumentsDirectory] stringByAppendingPathComponent: dbFilename];
NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: dbFilename];
NSLog(@"userPath %@", userPath);
NSLog(@"srcPath %@", srcPath);
NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL foundInBundle = [fileManager fileExistsAtPath: srcPath];
BOOL foundInUserPath = [fileManager fileExistsAtPath: userPath];
BOOL copySuccess = FALSE;

NSError *error;

if(foundInBundle)
{
    if(!foundInUserPath)
    {
        NSLog(@"don't have copy, copy one");
        copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];
    }
    if(!copySuccess)
    {
        NSLog(@"Failed with message: %@'.", [error localizedDescription]);
    }
}
else
{
    NSLog(@"Some error");
}
return YES;
}

根据foundInBundle,CoordinatesDatabase.sql 存在于应用程序包中。为什么不能“copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];”成功了吗?

它产生的错误类似于“无法完成操作。可可错误 4”。根据我的谷歌搜索,Cocoa Code Error 4 表示 NSFileNoSuchFileError 但我确定 CoordinatesDatabase.sql 存在于应用程序包中。

还是我错了,CoordintesDatabase.sql 确实存在于应用程序包中?

【问题讨论】:

    标签: ios cocoa file sqlite copy-item


    【解决方案1】:

    您无法创建、删除或更改应用程序包中的文件。您必须将文件写入正确的文档或缓存文件夹。这也有点复杂,因为 iOS4 和 iOS5 之间的规则发生了变化。

    您的应用也可能因在错误文件夹中创建文件而被拒绝。我最终编写了一种方法来根据他们正在运行的操作系统版本来处理文档文件夹的创建......并且当用户升级手机中的操作系统时,它支持将数据从旧文件夹迁移到新文件夹。你也应该这样做。

    我建议您查看此处处理此问题的其他问题:iOS PhoneGap app rejected because of the use of localStorage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-02
      • 2012-09-02
      • 2014-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多