簡體   English   中英

iOS將文件從主包復制到文檔目錄

[英]iOS copy files from main bundle to documents directory

我正在嘗試將我添加到名為“includes”的文件夾中的文件復制到名為“includes”的文檔目錄中的文件夾中。

我得到resContents的零值。 為什么?

- (void)copyResources{

    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"includes"];
    NSString *destPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"includes"];

    NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL];

    for (NSString* obj in resContents){
        NSError* error;
        if (![[NSFileManager defaultManager] copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj] toPath:[destPath stringByAppendingPathComponent:obj] error:&error]) {
            NSLog(@"Error: %@", error);;
        }
    }
}

您的Xcode項目應將您的includes文件夾添加為文件夾參考而不是

組只是為了保持組織而不是提供文件夾結構,因此在復制到設備時,所有文件最終都處於同一級別。

查看已編譯的應用程序包。

通常,Xcode生成的包是扁平的。 這意味着雖然您添加的資源文件將被復制到捆綁包,但您創建的任何目錄都不會,因此資源路徑中沒有“包含”目錄。 因此,您的源內容將為零。

所以在你的情況下,嘗試使用:

NSString *sourcePath = [[NSBundle mainBundle] resourcePath];

編輯:嗯,顯然添加文件夾引用也有效(信用Ignacio Inglese)。

暫無
暫無

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

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