gpt4 book ai didi

ios - 为什么 copyItemAtPath :toPath:userPath failed to copy the file when the file really exists? NSFileNoSuchFileError,Cocoa 错误代码 4

转载 作者:行者123 更新时间:2023-12-03 16:47:03 25 4
gpt4 key购买 nike

- (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,CooperativeDatabase.sql存在于应用程序包中。为什么不能“copySuccess = [fileManager copyItemAtPath: srcPath toPath: userPath error: &error];”会成功吗?

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

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

最佳答案

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

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

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

关于ios - 为什么 copyItemAtPath :toPath:userPath failed to copy the file when the file really exists? NSFileNoSuchFileError,Cocoa 错误代码 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12823090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com