简体   繁体   English

存储到plist只能在模拟器上进行,而不能在设备上进行

[英]Storing to plist works on simulator but not on device

I currently add data to a NSMutableArray and then read back from it to populate a UITableview. 我目前将数据添加到NSMutableArray,然后从中读取以填充UITableview。 This works fine on the simulator, but when testing on the device it doesnt work. 这在模拟器上工作正常,但是在设备上进行测试时不起作用。 The app still reads from the plist, so if I manually add data to it the app displays it in the tableview. 该应用程序仍从plist读取数据,因此,如果我手动向其添加数据,则该应用程序将其显示在tableview中。 But Cannot read or write to it on app ? 但是无法在应用程序上对其进行读写吗?

I use the current code 我使用当前代码

newTitle = [NSString stringWithFormat:@"%d_%@",[Daily_QuoteViewController counter], t];
[plistArray addObject:newTitle];
[plistArray writeToFile:filepath atomically: YES];

I have read that the list needs to be serialized as well, but others say this isnt necessary? 我已经读过该列表也需要序列化,但是其他人说这不是必需的吗?

Any help is appreciated. 任何帮助表示赞赏。

Thanks 谢谢

Are you sure your app has write permissions to the location you are trying to write the plist to? 您确定您的应用程序对尝试将plist写入的位置具有写入权限吗? There are differences between the file system access on the simulator and the actual device. 模拟器上的文件系统访问与实际设备之间存在差异。

Update : You can read more about the folders available for your app in iPhone Application Programming Guide: A Few Important Application Directories . 更新 :您可以在iPhone应用程序编程指南:一些重要的应用程序目录中阅读有关您的应用程序可用文件夹的更多信息。 The standard way of getting the locations of these folders is described in iPhone Application Programming Guide: Getting Paths to Application Directories . iPhone应用程序编程指南:获取应用程序目录的路径》中介绍了获取这些文件夹位置的标准方法。 Here's an example how to get the Documents folder and construct a path to a file in it: 这是一个如何获取Documents文件夹并在其中构造文件路径的示例:

NSArray *paths = NSSearchPathForDirectoriesInDomains(
                     NSDocumentDirectory,
                     NSUserDomainMask, YES
                 );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSURL *urla = [NSURL fileURLWithPath:[documentsDirectory
                         stringByAppendingString:@"/myinfo.plist"]];

Which application folder you will use depends on the actual content of your file and the intended use. 您将使用哪个应用程序文件夹取决于文件的实际内容和预期用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM