使用的plist字典应用程序设置 [英] Use a Plist dictionary for App settings

查看:201
本文介绍了使用的plist字典应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用的plist的设置即时通讯在我的应用程序实施。我想要一本字典设置握住我的阵列,如调试,选项1,选项2下阵设置字典等我怎么会进入调试?

 的NSArray *路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
的NSString * documentsDirectory = [路径objectAtIndex:0];
* NSString的myPlistPath = [documentsDirectory stringByAppendingPathComponent:@ProfileManager.plist];
*的NSDictionary = plistDict [[NSDictionary的页头] initWithContentsOfFile:myPlistPath];swDebug.on = [plistDict objectForKey:@调试];

的.plist

 <?XML版本=1.0编码=UTF-8&GT?;
!< D​​OCTYPE的plist PUBLIC - //苹果// DTD PLIST 1.0 // ENhttp://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<的plist版本=1.0>
<&字典GT;
<密钥GT;当前配置文件和LT; /键>
<串GT;样品档案< /串>
<密钥GT;自定义情景< /键>
<阵列GT;
    <&字典GT;
        <密钥GT;自动锁闭< /键>
        <假/>
        <密钥GT;蓝牙LT; /键>
        <假/>
        <密钥GT;&说明LT; /键>
        <字符串物实施例个人资料由应用&LT提供的; /串>
        <密钥GT;&边LT; /键>
        <假/>
        <密钥GT;手机和LT; /键>
        <假/>
        <密钥GT推送LT; /键>
        <假/>
        <密钥GT;铃声< /键>
        <串GT; Example.m4r< /串>
        <密钥GT;3克< /键>
        <假/>
        <密钥GT;标题< /键>
        <字符串物实施例简介< /串>
        <密钥GT;振动和LT; /键>
        <假/>
        <密钥GT; Wifi和LT; /键>
        <假/>
    < /字典>
    <&字典GT;
        <密钥GT;自动锁闭< /键>
        <假/>
        <密钥GT;蓝牙LT; /键>
        <假/>
        <密钥GT;&说明LT; /键>
        <串GT;应用程序&LT提供了样本配置文件; /串>
        <密钥GT;&边LT; /键>
        <假/>
        <密钥GT;手机和LT; /键>
        <假/>
        <密钥GT推送LT; /键>
        <假/>
        <密钥GT;铃声< /键>
        <串GT; Sample.m4r< /串>
        <密钥GT;3克< /键>
        <假/>
        <密钥GT;标题< /键>
        <串GT;样品档案< /串>
        <密钥GT;振动和LT; /键>
        <假/>
        <密钥GT; Wifi和LT; /键>
        <假/>
    < /字典>
< /阵列>
<键>设置< /键>
<&字典GT;
    <密钥GT;调试和LT; /键>
    <串GT;针对< /串>
< /字典>
< /字典>
< / plist中>


解决方案

在您的例子调试是一个字符串,而不是一个数组(这是你仿佛说,这是在问题)。在这两种情况下,问题是,你正在访问错误的字典的关键。您有:

  swDebug.on = [plistDict objectForKey:@调试];

您需要:

  swDebug.on = [[plistDict objectForKey:@设置] objectForKey:@调试];

由于设置是字典的plist内的字典。

I want to be able to use a plist for settings Im implementing in my app. I want a dictionary "Settings" to hold my arrays, such as "Debug", "Option 1", Option 2", etc. How would I access "Debug"array under the "Settings" dictionary?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:@"ProfileManager.plist"]; 
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:myPlistPath]; 

swDebug.on = [plistDict objectForKey:@"Debug"];

.plist

<?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">
<dict>
<key>Current Profile</key>
<string>Sample Profile</string>
<key>Custom Profiles</key>
<array>
    <dict>
        <key>autolock</key>
        <false/>
        <key>bluetooth</key>
        <false/>
        <key>desc</key>
        <string>Example profile provided by the application</string>
        <key>edge</key>
        <false/>
        <key>phone</key>
        <false/>
        <key>push</key>
        <false/>
        <key>ringtone</key>
        <string>Example.m4r</string>
        <key>threeg</key>
        <false/>
        <key>title</key>
        <string>Example Profile</string>
        <key>vibrate</key>
        <false/>
        <key>wifi</key>
        <false/>
    </dict>
    <dict>
        <key>autolock</key>
        <false/>
        <key>bluetooth</key>
        <false/>
        <key>desc</key>
        <string>Sample profile provided by the application</string>
        <key>edge</key>
        <false/>
        <key>phone</key>
        <false/>
        <key>push</key>
        <false/>
        <key>ringtone</key>
        <string>Sample.m4r</string>
        <key>threeg</key>
        <false/>
        <key>title</key>
        <string>Sample Profile</string>
        <key>vibrate</key>
        <false/>
        <key>wifi</key>
        <false/>
    </dict>
</array>
<key>Settings</key>
<dict>
    <key>Debug</key>
    <string>ON</string>
</dict>
</dict>
</plist>

解决方案

In your example Debug is a string, not an array (which is what you seem to say it is in the question). In either event, the issue is that you are accessing the key in the wrong dict. You have:

swDebug.on = [plistDict objectForKey:@"Debug"];

You would need:

swDebug.on = [[plistDict objectForKey:@"Settings"] objectForKey:@"Debug"];

Since settings is a dictionary within the dictionary plist.

这篇关于使用的plist字典应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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