アプリ設定にPlist辞書を使用する
-
05-07-2019 - |
質問
アプリに実装する設定にplistを使用できるようにしたい。辞書「設定」が欲しい「デバッグ」、「オプション1」、「オプション2」などの配列を保持します。「設定」の下の「デバッグ」配列にアクセスするにはどうすればよいですか。辞書?
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>
解決
あなたの例では、デバッグは文字列であり、配列ではありません(これはあなたが質問で言っているようです)。いずれにしても、問題は、間違った辞書のキーにアクセスしていることです。次のものがあります:
swDebug.on = [plistDict objectForKey:@"Debug"];
必要なもの:
swDebug.on = [[plistDict objectForKey:@"Settings"] objectForKey:@"Debug"];
設定は辞書plist内の辞書であるため。
所属していません StackOverflow