2011-03-24 80 views
0

我正在使用文件管理器來檢查文件的大小,代碼在模擬器上工作正常,但是當我在iPhone上運行相同的應用程序時,它說文件大小爲零,即使文件大小大於零,我使用以下代碼:在我的iPhone應用程序中的文件管理器

NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory1 = [paths1 objectAtIndex:0]; 
documentsDirectory1 = [documentsDirectory1 stringByAppendingPathComponent:@"XML"]; 
NSString * szDestPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"Extras"]; 
NSString *URL2 = [szDestPath1 stringByAppendingPathComponent:@"config.xml"]; 


NSLog(@"%@",URL2); 
NSError *attributesError = nil; 
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL2 error:&attributesError]; 

int _fileSize = [fileAttributes fileSize]; 

NSLog(@"_fileSize:%U",_fileSize); //0 if empty 

我該如何解決這個問題,誰能幫助我? thanx提前。

+0

您的格式有點上的代碼打破。閱讀這樣的非格式化代碼非常困難。你能幫我們解決嗎?謝謝。 – 2011-03-24 12:12:26

回答

1

您確定該文件存在嗎?

通常,如果一段涉及文件的代碼在模擬器上正常工作,但不在設備上,這是因爲文件名錯誤。大多數模擬器在不區分大小寫的文件系統上運行,但該設備具有區分大小寫的文件系統。

因此,如果您使用正確的文件路徑,請再次檢查。

您可以添加這樣的事情,以確保該文件存在:

if (![[NSFileManager defaultManager] fileExistsAtPath:URL2]) { 
    NSLog(@"File does not exist"); 
} 
+0

感謝您的回覆..它真的有效.. – sujay 2011-03-25 05:10:29

相關問題