2012-07-18 60 views
0

在Xcode的「支持文件」中包含一個名爲taxrates.csv的文件。無法使用[NSString stringWithContentsOfFile]加載文件?

NSString *fileString = [NSString stringWithContentsOfFile:@"taxrates.csv" encoding:NSUTF8StringEncoding error:NULL]; 
NSLog(@"%@",fileString); // <-- just displays null 
+3

我敢打賭,'錯誤'參數可能是有用的。 – 2012-07-18 22:29:11

回答

4

這是因爲應用程序的當前目錄不是應用程序的路徑,而是它包含的UUID文件夾的路徑。

要解決此問題,請嘗試使用[[NSBundle mainBundle] pathForResource:@"taxrates" ofType:@"csv"]來獲取文件的路徑而不是使用原始字符串。

+0

@jdl如果文件存在於你的目標中,它不應該返回null。 – 2012-07-18 22:31:44

+0

作品!謝謝! – jdl 2012-07-18 22:32:42

+0

@jdl樂於提供幫助。只記得接受&投票的答案:) – 2012-07-18 22:33:51