2012-05-19 180 views
0

我发现了一些类似的话题,但没有一个解决我的问题。 基本上我想从plist中读取我的数组中的nsstring,然后我想覆盖nsstring的值。数组无法从plist读取数据

NSArray *directoriespaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDir = [directoriespaths objectAtIndex:0]; 
NSString *fullnamefordir = [documentsDir stringByAppendingPathComponent:@"data.plist"]; 

     NSArray *turns = [NSArray arrayWithContentsOfFile:fullnamefordir]; 
     NSString *resultofarray = [turns objectAtIndex:0]; 
     NSLog(@"%@",turns); 
     NSLog(@"%@",resultofarray); 

的NSLog总是显示为空,我并不想保存任何到目前为止,因为我甚至无法读取值:/

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <string>8</string> 
</array> 
</plist> 

我也试图与NSDictionary中,但它也没有按” t工作 在此先感谢:)

回答

3

首先检查您的plist文件是否存在于文档目录中。

NSFileMangaer *objFileManager=[[NSFileManager alloc]init]; 
BOOL isfileExist=[objFileManager fileExistsAtPath:yourDocumentDirectoryPath]; 

IF布尔值是true,然后检查你的内容的plist的。 可能文件不存在于文档目录中,它在资源中。 如果您没有将plist从资源文件夹复制到文档目录,请先将其复制到那里,然后尝试访问它。

+0

nslog显示该文件确实存在。将文件从资源复制到文档目录是什么意思,我在哪里可以找到这个文件夹? – repoguy

+1

检查你的plist文件的文档目录。 YourUsername-> Library-> ApplicationSupport-> IphoneSimulator-> 5.0-> ChooseApplicationID-> Documents 如果有许多应用程序availabel删除所有并运行应用程序..只有一个应用程序将在那里,然后检查文档目录。 – Cyrus1

+0

就像你说的,我删除了所有的应用程序文件,我再次构建我的应用程序。奇怪的是,现在我的plist文件不存在于Documents中,并且nslog显示文件不存在。在这种情况下我该怎么办?顺便谢谢你的回复。 – repoguy