gpt4 book ai didi

iphone - cocoa 错误 : 516 while making writable copy of the SQLite database

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

我正在做一个小型数据库驱动的应用程序。我在 xcode 的 Supporting Files 下有我的 SQLite db 文件。起初,当它在主包中时,我试图向它写入数据,但不能。搜索后发现this回答,我将代码更改为以下内容。

- (void)viewDidLoad
{
[super viewDidLoad];

BOOL success;
NSFileManager *filemngr = [[NSFileManager alloc] init];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *writableDbPath = [documentDirectory stringByAppendingPathComponent:@"contacts.sqlite"];
success = [filemngr fileExistsAtPath:writableDbPath];
if(!success)
{
[status setText:@"Error occurred!"];
}

NSString *defaultDbPath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"contacts.sqlite"];
success = [filemngr copyItemAtPath:defaultDbPath toPath:writableDbPath error:&error];
if (!success)
{
[status setText:[error localizedDescription]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}

当我尝试将文件复制到可写位置(显示 UIAlertView 的位置)时,我在最后的 if block 中收到以下错误。

操作无法完成。 ( cocoa 错误 516)

谁能告诉我如何纠正这个错误?

谢谢你。

最佳答案

如果尚未将文件复制到那里,您希望将文件复制到文档目录。
在您的代码中,如果文件已经存在,那么您也尝试将其复制到文档目录。这将导致执行第二个 if(!success)堵塞。
改变你的方法,如:

- (void)viewDidLoad
{
[super viewDidLoad];

BOOL success;
NSFileManager *filemngr = [[NSFileManager alloc] init];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *writableDbPath = [documentDirectory stringByAppendingPathComponent:@"contacts.sqlite"];
success = [filemngr fileExistsAtPath:writableDbPath];
if(!success)
{
NSString *defaultDbPath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"contacts.sqlite"];
success = [filemngr copyItemAtPath:defaultDbPath toPath:writableDbPath error:&error];
if (!success)
{
[status setText:[error localizedDescription]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
}

关于iphone - cocoa 错误 : 516 while making writable copy of the SQLite database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950762/

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