存储在plist中的数据只能在模拟器中使用,而不能在设备中使用 [英] Data storing in plist works in simulaor but not in device

查看:90
本文介绍了存储在plist中的数据只能在模拟器中使用,而不能在设备中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iPhone开发的新手.我已经从先前的帖子中创建了plist .它在模拟器中效果很好,但在设备中效果不佳.

I am new to iPhone development. I have created plist such as from my previous post. It works well in simulator but not in device.

我正在从plist中获取保存的值并检查条件.当我使用模拟器时,它可以工作,但不能在设备中使用.

I am getting the saved value from the plist and checking for the condition. When I use simulator it works but not in device.

推荐答案

(1)您无法写入iPhone资源文件夹中的文件.它是电话安全系统的一部分,可防止恶意代码在安装后更改应用程序.

(1) You can't write to a file in the resource folder of an iPhone. It is part of the security system of the phone that prevent malicious code from altering an application after it has been installed.

(2)如果要将新数据保存到文件,则需要将该文件写入自动生成的文件夹之一.只要在设备或模拟器上安装了iPhone应用程序,系统就会创建一组默认文件夹.

(2) If you want to save new data to a file you need to write the file to one of the automatically generated folders. Whenever an iPhone app is installed on the device or simulator, the system creates a default set of folders.

(3)您要写入首选项"文件夹或文档"文件夹.如果数据与应用程序的操作有关,请将其写入首选项.如果包含用户数据,请写入文档.

(3) You want to write to either the Preferences folder or the Documents folder. If the data concerns the operation of the app, write it to preferences. If it contains user data write to Documents.

首选项:

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask, YES); 
NSString *prefsDirectory = [[sysPaths objectAtIndex:0] stringByAppendingPathComponent:@"/Preferences"];

文档:

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory ,NSUserDomainMask, YES); 
NSString *docsDirectory = [sysPaths objectAtIndex:0];

说您想读取默认的首选项plist文件,进行一些更改,然后将其保存在首选项文件夹中.

Say you want to read a default preference plist file, make some changes and then save it the preferences folder.

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"];
NSMutableArray *defaultPrefs=[[NSMutableArray alloc] initWithContentsOfFile:plistPath];
//... modify defaultPrefs
NSString *outputFilePath=[prefsDirectory stringByAppendingPathComponent:@"alteredPrefs.plist"];
[defaultPrefs writeToFile:outputFilePath atomically:NO];

这篇关于存储在plist中的数据只能在模拟器中使用,而不能在设备中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆