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

标签 iphone objective-c ios sqlite ios5

我正在做一个小型数据库驱动的应用程序。我在 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/

相关文章:

iphone - 关于iphone中的Paypal支付? (说明如下)

javascript - 在 iphone 的 webview 上自动显示键盘到下一个输入文本

iPhone 编程——UIView 元素似乎忽略了 Hidden=NO 调用

ios - 如何获取所有已注册设备(APNS)的设备 token ?

ios - 为什么我的模态视图的尺寸与我期望的不同?

ios - 如何为大型 UIScrollView(随 View 滚动)设置背景图案

ios - 如何在首次加载时选择 UIPickerView 中的第一行?

ios - 使用日历事件调用 ios 应用程序

objective-c - 重复使用细胞... :(

objective-c - 处理 View Controller 或 subview 中的通知