2011-03-23 106 views
0

在我的應用程序中,我必須讀取一個XML文件並將其顯示在UITextView中。爲此,我嘗試了下面的代碼。此代碼導致崩潰;任何人都可以告訴我確切的錯誤?我希望我寫得很好。請幫助我。謝謝。使用stringWithContentsOfFile讀取文件的問題:

-(IBAction)xmlParse{ 

    NSString *xmlPath=[[[[UIApplication sharedApplication]delegate] applicationDocumentDirectory]stringByAppendingPathComponent:@"xmlFile/toc.xml"]; 
    NSError *error=nil; 
    NSString *fileContent=[NSString stringWithContentsOfFile:xmlPath usedEncoding:NSASCIIStringEncoding error:NULL]; 
    xmlDisplay.text=fileContent; 

} 
+0

請加上控制檯錯誤消息。 – 2011-03-23 13:37:55

回答

3

更換

NSString *fileContent=[NSString stringWithContentsOfFile:xmlPath usedEncoding:NSASCIIStringEncoding error:NULL]; 

NSString *fileContent=[NSString stringWithContentsOfFile:xmlPath encoding:NSASCIIStringEncoding error:NULL]; 

你不應該忽視assing argument 2 of 'stringWithContentsOfFile:usedEncoding:error:' makes pointer from integer without a cast警告編譯器會顯示。

+0

+1謝謝你的工作,你可以給我更詳細的兩個報表比較。 – ajay 2011-03-23 13:44:45

+0

用'usedEncoding:'方法試圖找出你的編碼。它會將使用的編碼分配給您提供的指針。所以該方法試圖爲地址1(NSASCIIStringEncoding的值)賦值。由於這是一個你不應該寫的地址,你會得到一個不好的訪問異常。有關更多詳細信息,請參閱文檔。 – 2011-03-23 13:57:25

+0

再次感謝你fluchtpunkt .. – ajay 2011-03-23 14:05:39

1

我以前面臨同樣的問題。它是保存xml文件內的文件目錄本身不在任何子文件夾.try中保存到文檔目錄。希望應該工作....

或U可以嘗試

-(IBAction)xmlParse{ 

NSString *xmlPath=[[[[UIApplication sharedApplication]delegate] applicationDocumentDirectory]stringByAppendingPathComponent:@"xmlFile/toc.xml"]; 
NSError *error=nil; 
NSString *fileContent=[NSString stringWithContentsOfFile:xmlPath usedEncoding:NSASCIIStringEncoding error:NULL]; 
if(nil!=fileContent) 
xmlDisplay.text=fileContent;