Cocoa错误:516同时制作SQLite数据库的可写副本

时间:2012-12-19 10:54:42

标签: iphone objective-c ios sqlite ios5

我正在做一个小型数据库驱动的应用程序。我在xcode中的支持文件下有我的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块中收到以下错误。

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

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

谢谢。

1 个答案:

答案 0 :(得分:3)

如果尚未将文件复制到文件目录,则需要将文件复制到文件目录。 在您的代码中,如果文件已经存在,那么您也尝试将其复制到文档目录。这将导致第二个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];
          }
        }
    }