2012-12-05 40 views
0

如果一個文件的文件夾裏面有我有一個奇怪的問題,該代碼檢查:文件中的文件夾存在,但不是Xcode的

- (BOOL) checkIfFileExist:(NSString *)path {  

    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentsPaths objectAtIndex:0]; 
    NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:fileDaControllere]) { 
     NSLog(@"exist"); 
     return YES; 
    } 
    else { 
     NSLog(@"not exist"); 
     return NO; 
    } 
} 

的問題是,我得到送花兒給人文件不同時,該文件存在存在(在這種情況下,路徑是Style.css)!錯誤在哪裏? 的路徑似乎是正確的:

路徑:/用戶/ kikko /庫/應用程序支持/ iPhone模擬器/ 6.0 /應用/ 38161AFA-2740-4BE2-9EC4-C5C6B317D270 /文檔/ style.css中

在這裏你可以看到

http://www.allmyapp.net/wp-content/iFormulario/1.png

http://www.allmyapp.net/wp-content/iFormulario/2.png

+0

1 - 你確定文件在文件目錄?你在那裏複製/創建? 2-檢查文件名,區分大小寫Style.css或style.css – aahsanali

+0

您是否嘗試在** real **設備上進行測試?模擬器沒有絕對的權利訪問Mac文件系統,包括應用程序的文檔文件夾,儘管文件真的存在,並且在** real **設備上訪問它也沒有問題,但它可能會導致一些小問題。 – holex

+0

@holex我在許多應用程序中使用了上面的代碼,它們都可以在模擬器上完美地工作,除了大多數Mac的文件系統不區分大小寫,而iOS上的文件系統則不區分大小寫。 – rckoenes

回答

0

在我解決了這個問題,最終的特殊方法,奇怪想到的是,我不知道我是如何解決它, 的第一個問題是,我傳遞給checkIfFifFileExist的絕對路徑,而我需要傳遞它的相對路徑,並將該函數轉換爲絕對路徑(我的大錯誤),之後我認爲問題是使用「/」,我刪除了所有的重寫所有的代碼,我做了一些測試。

我複製文件夾從包:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [documentPaths objectAtIndex:0]; 

NSString *folderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"/icone"]; 
NSString *iconePath = [documentsDir stringByAppendingPathComponent:@"/icone"]; 

[[NSFileManager defaultManager] copyItemAtPath:folderPath toPath:iconePath error:nil]; 

然後,我讓我的形象的路徑是這樣的:

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [documentPaths objectAtIndex:0]; 
NSString *imagePath = [documentsDir stringByAppendingPathComponent:self.objMateria.iconaMateria]; 

,現在該文件存在,一件奇怪的事情是,如果:

self.objMateria.iconaMateria = /icona/Home/fisica.png 

self.objMateria.iconaMateria = icona/Home/fisica.png 

沒什麼變化,我看到的形象,而我認爲,這其中有一個錯誤的道路......

0

問題可能是事實,你只是追加的文件,而無需與檢查,如果在Xcode的路徑和真實路徑有一條小路delimitor:

NSString *fileDaControllere = [documentsDirectory stringByAppendingString:path]; 

因此,你會成爲像../DocumentsStyle.css但它應該是../Documents/Style.css

NSString有追加的路徑組件stringByAppendingPathComponent:

NSString *fileDaControllere = [documentsDirectory stringByAppendingPathComponent:path]; 
+0

謝謝,我試用這種方法,但是是一樣的,文件不存在,而明顯存在文件(我在文檔目錄中看到它) – kikko088

+0

添加一個'NSLog',在其中打印'fileDaControllere'並檢查路徑是否正確。 – rckoenes

+0

然後在真正的設備上測試它,因爲holex說'NSFileManager'可能有問題。 – rckoenes

相關問題