2016-07-23 83 views
0

我遇到了一個問題,從項目中找到一個文件。我使用代碼獲取這個文件,但是我無法使用url獲取它。如何在Project中查找文件?

請幫我解決這個問題。

下面是文件的路徑

用戶/ yatish /庫/開發商/ CoreSimulator /設備/ 65048208-DC46-4B30-9526-470D6D8081D3 /數據/集裝箱/數據/應用/ 2E7EFDA0-9C0C- 4440-962D-610C2AC18DDD /文檔/ file.txt的

下面是代碼

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *Path = [NSString stringWithFormat:@"%@/%@%@",documentsDirectory,@"file"@".txt"]; 
[str writeToFile:Path atomically:YES encoding:NSUTF8StringEncoding error:&error]; 

NSString *[email protected]"file"; 
NSString *searchFilename =[fn stringByAppendingString:@".txt"]; // name of the PDF you are searching for 
NSArray *pathss = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectorys = [pathss objectAtIndex:0]; 
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectorys]; 
NSString *documentsSubpath; 
BOOL fileFound = false; 

while (documentsSubpath = [direnum nextObject]) 
{ 
    if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) { 
     continue; 
     fileFound=NO; 
    } 

    NSLog(@"found %@", documentsSubpath); 
    fileFound =YES; 
} 
if(fileFound){ 
    NSLog(@"File Found");///this line is execute when i run this code 
} 
+0

我已經在finder中找到文件,但沒有顯示文件 –

回答

0

在代碼中,你可以使用:

NSURL *fileURL = [NSURL URLWithPath: documentsSubpath]; 

而這將是您的文件的路徑。

如果你想看到實際的文件,去Macintosh查找並點擊「轉到文件夾」(它是「開始」菜單下,或一起)。然後鍵入你的路徑MINUS最後一個路徑組件,它是文件名。在你的情況下,這是「/Users/yatish/Library/Developer/CoreSimulator/Devices/65048208-DC46-4B30-9526-470D6D8081D3/data/Containers/Data/Application/2E7EFDA0-9C0C-4440-962D-610C2AC18DDD/Documents」。

+0

此代碼已經工作,但我想看到文件在物理上它在哪裏。我在finder中搜索這個路徑,並且在這裏找到了Shit + Command + G,但是沒有找到文件,我錯了,請告訴我... –

+0

你錯過了「'/ Users'」之前的斜線。 –

相關問題