2011-05-16 35 views
0

在我的资源文件添加新项目的plist我已经创建了一个plist中,我将项目添加到PList 为EXM:通过使用设备

plistarray=[NSMutableArray arrayWithContentsOfFile:@"/Users/mangomac2/Desktop/chat module/complete (iphoneDesign) 2/picture-vide 2/Data.plist"]; 
    if (plistarray==nil) 

     plistarray=[NSMutableArray array]; 
    [plistarray addObject:datadict]; 
    [plistarray writeToFile:@"/Users/mangomac2/Desktop/chat module/complete (iphoneDesign) 2/picture-vide 2/Data.plist" atomically:YES]; 
    [datadict release]; 

我的数据添加到plist中,但每当即时上传我的代码对其设备不添加plist也不检索plist。

请告诉我我在哪里犯错误。

回答

0

首先,您对应用程序进行硬编码,而不考虑应用程序将安装在设备模拟器上的不同位置以及可能位于每个不同设备上的不同位置好。第二,似乎你正在尝试更新应用程序包内的文件;这是不允许的,并且如果iOS使用文件系统权限强制执行应用程序包的不可写入性,则可能会失败。

要在您的包中找到文件,应该使用NSBundle类的相应方法。例如,要获取文件Data.plist的路径,可以使用[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]

如果您想要修改此文件,您必须先将其复制到可写位置(然后将来从此位置而不是从您的包中读取)。您可以使用NSSearchPathForDirectoriesInDomains获得适当的目录。例如,[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]会为您提供应用程序Documents目录的路径。然后,您可以使用类似[directory stringByAppendingPathComponent:@"Data.plist"]的文件来获取可存储Data.plist的位置的完整路径和文件名。

相关问题