簡體   English   中英

嘗試從分發包復制到文檔目錄時出錯

[英]Error trying to copy from bundle to documents directory

我已經在應用程序中添加了一個sqlite文件,並試圖將其從分發包復制到documents目錄。 我也將sqlite添加到了目標應用程序中。 以下是我用來復制文件的代碼:

NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:destination]) {
        NSString *source = [[NSBundle mainBundle] pathForResource:@"myapp" ofType:@"sqlite"];
        NSError *error = nil;
        [fileManager copyItemAtPath:source toPath:destination error:&error];
        if (error) {
            //
        }
    }

[Utils applicationDocumentsDirectory]代碼:

+ (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

但是當執行此代碼時,出現以下error

錯誤域= NSCocoaErrorDomain代碼= 4“文件“ myapp.sqlite”不存在。” 的UserInfo = {NSSourceFilePathErrorKey = /用戶/ harikrishnant /庫/開發商/ CoreSimulator /設備/ 8E531314-F1AE-417F-8E99-7AA92967CDC9 /數據/容器/捆綁/應用/ 0A710929-475D-457C-8D2D-C2F2BBEB6B92 / myapp.app / myapp.sqlite,NSUserStringVariant =(復制),NSDestinationFilePath = file:/// Users / harikrishnant / Library / Developer / CoreSimulator / Devices / 8E531314-F1AE-417F-8E99-7AA92967CDC9 / data / Containers / Data / Application / 13B22E15-D00C -433C-9F02-014B1F73D183 / Documents / myapp.sqlite,NSFilePath = / Users / harikrishnant / Library / Developer / CoreSimulator / Devices / 8E531314-F1AE-417F-8E99-7AA92967CDC9 / data / Containers / Bundle / Application / 0A710929-475D- 457C-8D2D-C2F2BBEB6B92 / myapp.app / myapp.sqlite,NSUnderlyingError = 0x7faffe1b5cf0 {Error Domain = NSPOSIXErrorDomain Code = 2“沒有此類文件或目錄”}}

我使用終端檢查了以下路徑,並發現sqlite文件實際上存在於應用程序捆綁包中:

/Users/harikrishnant/Library/Developer/CoreSimulator/Devices/8E531314-F1AE-417F-8E99-7AA92967CDC9/data/Containers/Bundle/Application/0A710929-475D-457C-8D2D-C2F2BBEB6B92/myapp.app/myapp.sqlite

但是我仍然收到此錯誤。 我嘗試了一切。 我什至清理了build文件夾並重新安裝了該應用程序,但仍然無法正常工作。 這可能是什么問題? 怎么解決呢?

您的代碼幾乎是正確的。 這條線有兩個缺陷:

NSString *destination = [[[Utils applicationDocumentsDirectory] absoluteString] stringByAppendingString:@"myapp.sqlite"];

使用absoluteString不正確。 這將以file://path/to/Documents/的形式提供文件URL。 您需要使用path方法來從NSURL獲取文件路徑(而非文件URL)。

然后,您需要將文件名正確附加到路徑中。 您需要使用stringByAppendingPathComponent:而不是stringByAppendingString:

該行應為:

NSString *destination = [[[Utils applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"myapp.sqlite"];

我使用下面的代碼將SQLite數據庫從Application Bundle復制到Document Directory。
注意 “數據庫”是文件夾名稱。 “數據源”是我的數據庫名稱,擴展名為“ db”

-(void)saveDatabase{
        //put bundle database to DocumentDirectory
        NSLog(@"%@",[self databasePath]);
        //Create Path For Destination
        NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        path = [path stringByAppendingPathComponent:@"Database"];
        BOOL isExists=[[NSFileManager defaultManager]fileExistsAtPath:path];
        if(!isExists){
            NSError* error;
            BOOL isCreated = [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
            if(isCreated){
                path = [path stringByAppendingPathComponent:@"Datasource"];
                path = [path stringByAppendingPathExtension:@"db"];
                isExists = [[NSFileManager defaultManager]fileExistsAtPath:path];
                if(!isExists){
                    NSError* dbError;
                    NSString* bundle = [[NSBundle mainBundle]pathForResource:@"Datasource" ofType:@"db"];
                    isCreated = [[NSFileManager defaultManager]copyItemAtPath:bundle toPath:path error:&dbError];
                    if(isCreated){
                        NSLog(@"DatabasePath %@",path);
                    }else{
                        NSLog(@"ErrorCopying DB file %@",dbError.localizedDescription);
                    }
                }else{

                }

            }else{
                NSLog(@"Error While Creating Database Directory:->%@",error.localizedDescription);
            }
        }
    }
    - (NSString*)databasePath{
        NSString* path = [self documentDirectory];
        path = [path stringByAppendingPathComponent:@"Database"];
        path = [path stringByAppendingPathComponent:@"Datasource"];
        path = [path stringByAppendingPathExtension:@"db"];
        return path;
    }
    - (NSString*)documentDirectory{
        return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    }

確保您使用的目標中的“ Build Phases -> Copy Bundle Resources中列出了您的文件。 嘗試刪除Derived data 如果仍然無法使用,請轉到“ Derived data然后使用Finder檢查文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM