Pergunta

Eu quero ser capaz de usar um plist para configurações Im implementar em meu aplicativo. Eu quero um dicionário "Configurações" para segurar minhas matrizes, tais como "depuração", "Opção 1", Opção 2" , etc. Como eu acessar '' array sob o 'Debug dicionário Configurações'?

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>
Foi útil?

Solução

No seu exemplo de depuração é uma string, não um array (que é o que você parece dizer que é na questão). Em qualquer caso, o problema é que você está acessando a chave na dict errado. Você tem:

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

Você precisaria:

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

Uma vez que as configurações é um dicionário dentro do plist dicionário.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top