Objective c 无法将文件从捆绑包复制到文档目录,为什么fileExistsAtPath返回错误?

Objective c 无法将文件从捆绑包复制到文档目录,为什么fileExistsAtPath返回错误?,objective-c,ios,ios5,nsfilemanager,Objective C,Ios,Ios5,Nsfilemanager,我正在使用以下代码片段尝试将文件从我的应用程序资源目录复制到文档区域。我使用了Github项目中的以下内容: // Set up the tessdata path. This is included in the application bundle // but is copied to the Documents directory on the first run. NSString *dataPath = [[self applicationDocumentsDirec

我正在使用以下代码片段尝试将文件从我的应用程序资源目录复制到文档区域。我使用了Github项目中的以下内容:

// Set up the tessdata path. This is included in the application bundle
    // but is copied to the Documents directory on the first run.
    NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSLog(@"Datapath is %@", dataPath);
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:dataPath ]) {
        NSLog(@"File didn't exist at datapath...");
        // get the path to the app bundle (with the tessdata dir)
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@"tessdata"];
        if (tessdataPath) {
            [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
        }
    }
这是我通过调用上述命令得到的输出:

2012-06-06 14:53:42.607 MyApp[1072:707]数据路径为 /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/文档/数据 打开数据文件时出错 /var/mobile/Applications/9676D920-D6D1-4F86-9177-07CC3247A124/Documents/tessdata/eng.traineddata

我的xcode项目中有eng.traineddata文件,如下所示

在尝试检查
fileExistsAtPath
时,似乎报告了错误,我不希望这会引发错误,但会返回YES或NO

有没有更简单的方法来复制
eng.traineddata
文件,或者我犯了一个可以在这里更正的错误


使用
NSFileManager
fileExistsAtPath:isDirectory:
方法,并指定路径是目录,因为它是。

stringByAppendingPathComponent:@“tessdata”


上面的一行要求tessdata是Xcode项目中的一个物理文件夹(蓝色文件夹),而不是一个组(显示的是黄色文件夹)。使用Finder在您的Xcode项目文件夹中创建一个文件夹,然后在Xcode中将文件夹拖放到您的项目中,并在需要时选择文件夹引用。

我希望这段代码也能帮助您将所有类型的文件包复制到文档目录文件夹中

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;

NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"];
NSLog(@"Datapath is %@", dataPath);

// If the expected store doesn't exist, copy the default store.    
    if ([fileManager fileExistsAtPath:dataPath] == NO)
    {
        NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"eng" ofType:@"traineddata"];
        [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:&error];

    }


-(NSString*) applicationDocumentsDirectory{
    // Get the documents directory
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    return docsDir;
}
NSString *pathsToReources = [[NSBundle mainBundle] resourcePath];
NSString *yourOriginalDatabasePath = [pathsToReources stringByAppendingPathComponent:@"lunchDB.sqlite"];  // file name u want copy 

NSArray *pathsToDocuments = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [pathsToDocuments objectAtIndex: 0];


    NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"lunchDB.sqlite"];  // file name & your original path (Document path)

    if (![[NSFileManager defaultManager] isReadableFileAtPath: dbPath]) {

if ([[NSFileManager defaultManager] copyItemAtPath: yourOriginalDatabasePath toPath: dbPath error: NULL] != YES)

NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalDatabasePath, dbPath);

}

谢谢。

什么是[自我应用文档目录]?缺少/不完整的代码?不完整的代码,包装路径查找代码的常用方法,请参见示例(尽管在本例中返回NSURL)