如何在iOS中将文件从文档复制到应用程序包中?

如何在iOS中将文件从文档复制到应用程序包中?,ios,cocoa-touch,Ios,Cocoa Touch,我想将文件从文档复制到iOS中的资源文件夹中 不是要记录的资源 从文档到资源的文件 所以我写了以下代码 - (NSString *) getDBPath2 { NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Htoo.mp3"]; return dbPath; } - (void) copyDatabaseIfNeeded {

我想将文件从文档复制到iOS中的资源文件夹中

不是要记录的资源

从文档到资源的文件

所以我写了以下代码

- (NSString *) getDBPath2 
{   
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Htoo.mp3"];

    return dbPath;
}

- (void) copyDatabaseIfNeeded 
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDBPath];
    BOOL success = [fileManager fileExistsAtPath:dbPath]; 

    if(success) 
    {   
        NSString *defaultDBPath = [[NSBundle mainBundle] resourcePath];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

        if (!success)
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }   
}

- (NSString *) getDBPath 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"Htoo.mp3"];
}
当我编写上述代码时,我收到了错误消息

2012-08-17 23:55:03.482 TestProjects[1645:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to create writable database file with message 'The operation couldn’t be completed. (Cocoa error 516.)'.'
那个么,如何在iOS中将文件从文档复制到资源中呢


感谢您的阅读。

您的应用程序(捆绑包)的资源是只读的,发布后您不能修改捆绑包。捆绑包是在编译时由Xcode创建的,您不能在运行时修改它。安装应用程序后,有很多方法可以存储数据:NSDefaults、sqlite、Core data、documents directory。

如果要保存MP3,最好将其保存在缓存中:

    NSString *desPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

你是说应用程序包?你没有。你做不到。它是只读的。iOS设备上的应用程序包是只读的。您这样做的目的是什么?但我可以将文本数据写入到放在捆绑包中的SQLITE3数据库中。仍然不清楚您为什么需要保存到应用捆绑包中。这是不允许的。您必须保存到应用程序的关联文档目录。