Verschachtelte Plist (mehrere Dictionaries) auslesen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Verschachtelte Plist (mehrere Dictionaries) auslesen

    Hey Zusammen,

    ich habe mal eine Anfänger-Frage bzgl. .plist-Dateien und das Auslesen mehrerer Dictionaries.

    Ich dachte ich könnte einfach mit simplen Schleifen immer "tiefer" in meiner plist navigieren.... das scheint aber nicht zu klappen und Google / Suche hier konnte mir nicht weiterhelfen. Ich hoffe ihr könnt mir kurz helfen.

    Aufbau meiner Plist:

    Quellcode

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    3. <plist version="1.0">
    4. <dict>
    5. <key>1</key>
    6. <dict>
    7. <key>id</key>
    8. <string></string>
    9. <key>settings</key>
    10. <dict>
    11. <key>name</key>
    12. <string>theName</string>
    13. <key>image</key>
    14. <string>imageName</string>
    15. </dict>
    16. </dict>
    17. <key>2</key>
    18. <dict>
    19. <key>id</key>
    20. <integer>2</integer>
    21. <key>settings</key>
    22. <dict>
    23. <key>name</key>
    24. <string>theName2</string>
    25. <key>image2</key>
    26. <string>imageName</string>
    27. </dict>
    28. </dict>
    29. </dict>
    30. </plist>
    Alles anzeigen



    Mein Code-Schnipsel:

    Quellcode

    1. NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"plist" ofType:@"plist"];
    2. NSDictionary* contentArray = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    3. NSLog(@"contentArray %@",contentArray);
    4. for (NSDictionary* dict in contentArray) {
    5. NSLog(@"dict: %@",dict);
    6. NSDictionary *first = [dict objectForKey:@"1"];
    7. for (NSDictionary* firstDict in first) {
    8. NSLog(@"firstDict: %@",firstDict);
    9. NSDictionary *settingsDict = [dict objectForKey:@"settings"];
    10. for (NSDictionary* settings in settingsDict) {
    11. NSLog(@"settings: %@",settings);
    12. }
    13. }
    14. }
    Alles anzeigen


    Ich freue mich auf eure Antworten!

    Gruß,
    Florian
  • Hmm... so? (klappt es leider auch nicht)

    Quellcode

    1. NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"plist" ofType:@"plist"];
    2. NSDictionary* contentArray = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    3. NSLog(@"contentArray %@",contentArray);
    4. NSDictionary *first = [contentArray objectForKey:@"1"];
    5. for (NSDictionary* dict in first) {
    6. NSLog(@"dict: %@",dict);
    7. NSDictionary *settingsDict = [dict objectForKey:@"settings"];
    8. for (NSDictionary* settings in settingsDict) {
    9. NSLog(@"settings: %@",settings);
    10. }
    11. }
    Alles anzeigen

  • 2014-06-26 14:47:08.223 plistTest[7441:1781442] dict: id
    2014-06-26 14:47:08.223 plistTest[7441:1781442] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x178226ba0
    2014-06-26 14:47:08.224 plistTest[7441:1781442] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x178226ba0'

    *** First throw call stack:
    (0x18230234c 0x1916f41e4 0x1823088b0 0x182306110 0x18220a36c 0x1000fec2c 0x1867e8d80 0x1867e8b04 0x1867eef58 0x1867ec7fc 0x18685dd68 0x186a74d18 0x186a76fb4 0x186a75718 0x1822bba88 0x1822bab90 0x1822b8d90 0x1821e4924 0x186853a94 0x18684ec3c 0x1000ff198 0x191d6ea08)
    libc++abi.dylib: terminating with uncaught exception of type NSException


    Eigentlich möchte ich nur die einzelnen Settings abfragen (beliebig viele) für eine bestimmte ID ... für diesen Test erstmal fest die 1. :)

    PS: Danke für die Mühe