2011-03-23 56 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;