gpt4 book ai didi

iphone - 使用 Plist 字典进行应用程序设置

转载 作者:行者123 更新时间:2023-12-03 20:02:26 25 4
gpt4 key购买 nike

我希望能够使用 plist 来进行我在应用程序中实现的设置。我想要一个字典“Settings”来保存我的数组,例如“Debug”、“Option 1”、Option 2”等。如何访问“Settings”字典下的“Debug”数组?

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>

最佳答案

在您的示例中,调试是一个字符串,而不是一个数组(这就是您在问题中所说的)。无论哪种情况,问题都是您在错误的字典中访问 key 。你有:

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

您需要:

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

因为设置是字典 plist 中的字典。

关于iphone - 使用 Plist 字典进行应用程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1801070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com