- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在资源文件夹中创建了 save.plist。我直接在里面写了一些数据(不使用编码)。我能够读取该数据,但无法将代码写入同一个 save.plist。通过使用以下代码,我尝试写入数据,但它存储在我的 .app plist 中。代码在这里
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"save" ofType:@"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
if (!temp) {
NSLog(errorDesc);
[errorDesc release];
}
// [temp setValue:@"123" forKey:@"line1"];
// [temp writeToFile:plistPath atomically: YES];
//Reading data from save.plist
NSLog([temp objectForKey:@"name"]);
NSLog([temp objectForKey:@"wish"]);
NSNumber *num=[temp valueForKey:@"roll"];
int i=[num intValue];
printf("%d",i);
//writitng the data in save.plist
[temp setValue:@"green" forKey:@"color"];
[temp writeToFile:plistPath atomically: NO];
NSMutableDictionary *temp1 = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
NSLog([temp objectForKey:@"color"]);
我想要的是,我想写入的数据应该只写入存储在引用中的 save.plist 中。我对这个概念很陌生。所以如果有人知道请帮助我。提前致谢。:-)
最佳答案
我不知道我是否理解你的问题,但如果你想写入你的 .app 包中的 .plist,你可能做错了什么。如果你想存储偏好,你应该考虑使用 NSUserDefaults .
如果你真的想修改一个捆绑的 .plist - 这里有一些代码:
NSString *plistPath = nil;
NSFileManager *manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Contents/Info.plist"])
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"LSUIElement"];
[infoDict writeToFile:plistPath atomically:NO];
[manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
}
}
更新:
Nate Flink 指出上面使用的一些 NSFileManager 方法已被弃用。他用下面的替换方法发布了一个答案: https://stackoverflow.com/a/12428472/100848
关于iphone - 如何在plist中写入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/905542/
我的应用程序中有一个 PLIST 文件,其中包含各种配置数据。一些数据是用于访问服务器的 URL。该服务器为我们代码的几个不同版本托管 JSON 文件。我想要做的是在具有版本的 PLIST 文件中有一
我想制作一个从 plist 中检索数据的数据管理器类,我想知道我是否应该制作一个包含所有类方法的类,每次调用该方法并返回请求的值时读取 plist,或者创建一个类使用 plist 数据初始化数组(实例
我正在编写一个生成 .xcodeproj 文件的应用程序。它生成了一个 .pbxproj 文件,但是在 Xcode 中打开它时,我收到一条错误消息,“无法打开,因为无法解析项目文件。”仅此而已。 是否
要在 iPhone 中执行我的应用程序,首先我必须将配置文件添加到我的 iPhone 中。好的。但我不明白在资源下的 xCode 中的 iphone 应用程序中添加 entitlements.plis
我目前正在尝试从 Visual Studio Community 2017 为 MAC 运行与 Appium 集成的 C# NUnit 测试脚本。软件配置为 MAC OS- 10.12.6、Appiu
通过网络发送二进制 plist 比简单 plist 的优点是什么。另外,客户端处理二进制 plist 的速度有多快? 最佳答案 与 XML 格式的 plist 相比,它们通常要小得多,而且处理速度要快
我有一个如下图所示的 plist,那是我的主要 plist,其中 “bid” 字段是唯一的。我有出值(value) (例如 bid=90) 然后我需要在我的主(第一张图片)plist 的所有条目中搜索
如果不使用主包中 plist 上文件管理器实例的 .copyItemAtPath 方法,我将如何创建一个空的 plist 文件?我想检查我的 DocumentDirectory 中是否已经创建了一个
我在 ~/Library/LaunchAgents 有以下 plist 文件: Label com.yogapo.test_launchd Program
所以我知道如何让苹果邮件 9 始终加载远程内容,但我需要以编程方式设置该首选项。我正在查看苹果邮件 plist 文件,但似乎找不到值。有谁知道这个设置可能在哪里? 最佳答案 我不同意 ~/Librar
使用 MonoTouch 加载和处理“两级嵌套”plist 文件的最佳方式是什么?我有一个 plist 文件,其中的数据(结构上)类似于以下内容: - USA --- New York --- Chi
所以在我的最后一个问题中,我试图弄清楚我应该使用什么,Plist 或 Core data 或 sqllight,我决定尝试所有这些来提高我的技能。所以我开始阅读有关 plist 及其工作方式的文章,但
我有一个手动维护的属性列表,它定义了一组相同类型的对象。这些对象具有许多属性,其中之一是正则表达式字符串。一些对象共享一个通用的正则表达式,我的问题是我必须在文件的多个位置维护相同的正则表达式,这很容
-(NSString*)dataFilePath{ NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentD
我正在尝试让我的应用程序从电子邮件中将设置导入到自身中。这将起作用的方式是用户将向自己发送一个 settings.properties 文件,这实际上是一个看起来像这样的 plist
我在 Xcode 4.2 中有一个应用程序。 我已经根据需要创建了 plist 文件。 我已经从应用程序主包中找到数据并将数据放入数组中。 但是由于我想在运行时修改plist数据,所以在Documen
Apple 强烈建议在将基于 XML 的大型数据集读入 iPhone 应用程序时使用二进制 plist 格式。他们的理由之一是 XML 解析在 iPhone 上非常费力。但是,这需要首先转换驻留在远程
我在 Xcode build设置中启用了 Info.plist 的预处理,以用 version.h 中的相应值替换 CFBundleVersion 中表示版本号的键。这很好用:VERSION_NUMB
抱歉,帖子太长了。当我第二次使用 plist 中的字符串时,它使我的程序崩溃。我有 cocos2d 项目( attach ),其中有一个场景和一个对象。和一个 .plist 文件 HelloWorld
我已将新实体添加到现有 Plist。由于 Plist 不一致,新版本的应用程序崩溃。 如何将旧 plist 数据与新 plist 合并? 我的Plist创建方法如下 - (void)createEdi
我是一名优秀的程序员,十分优秀!