1

我已经为此苦苦挣扎了一个多月,所以我试图自己解决这个问题。

我有一个包含项目字典的列表列表。例如,如何下钻以显示购物清单中项目的已检查状态?

<dict>
<key>Shopping List</key>
<array>
    <dict>
        <key>Name</key>
        <string>Green Tea</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Tempeh</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Kimuchi</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Jasmine Rice</string>
        <key>Checked</key>
        <false/>
    </dict>
</array>
<key>To-Do List</key>
<array>
    <dict>
        <key>Name</key>
        <string>Wash Car</string>
        <key>Checked</key>
        <false/>
    </dict>
    <dict>
        <key>Name</key>
        <string>Run Diswasher</string>
        <key>Checked</key>
        <false/>
    </dict>
</array>

4

2 回答 2

0

尝试这个 :

// First we get the path for the plist
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"PlistFileName" ofType:@"plist"]; // Replace PlistFileName with your plist name (without its file extension)

// Then, we get the first dictionary (containing "Shopping List" and "To-Do List") 
NSDictionary *listsDictionary = [[NSDictionary alloc] initWithContentsOfFile:thePath];

// Then, we get the array contained in the "Shopping List" object of the previous dictionary we got
NSArray *theShoppingList = [listsDictionary objectForKey:@"Shopping List"];

// Then, we get the dictionary representing an item of the list at a given index in the previous array we got
NSDictionary *item = (NSDictionary *)[theShoppingList objectAtIndex:index]; // index represents the index of the item in the plist

// Last but not least, we get the Checked value (as a boolean value) contained in the item
Boolean checked = [[item objectForKey:@"Checked"] boolValue];
于 2013-09-04T21:55:30.260 回答
0

当您将文件读入 NSDictionary 时,您将拥有这样的树形结构,

@{ 
  @"Shopping List" :
   @[
     @{
        @"Name": @"Green Tea",
        @"Checked": [NSNumber numberWithBool: NO]
    },
    @{
        @"Name": @"Tempeh", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
    @{
        @"Name": @"Jasmine Rice", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
  ],

  @"To-Do List":
  @[
    @{
        @"Name": @"Wash Car", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
    @{
        @"Name": @"Run Diswasher", 
        @"Checked": [NSNumber numberWithBool: NO]
    },
  ]
}

我使用新的 Objective C 字典文字从上面的 plist 创建了字典。这应该更有意义,并且可以很容易地解析事物。

因此,要显示是否选中了“绿茶”,您会这样做,

dict[@"Shopping List"][0][@"Checked"]

对于茉莉香米,

dict[@"Shopping List"][2][@"Checked"]

洗车,

dict[@"To-Do list"][0][@"Checked"]

我创建了一个小型辅助函数,您可以使用它枚举所有键并为某些键选择特定值,

- (void)enumerateDictionary:(NSDictionary*)dictionary{
  for(NSString *key in dictionary){
    id object = [dictionary valueForKey:key];
    if([object isKindOfClass:[NSArray class]])
      for(id dict in object)
        [self enumerateDictionary:dict];
    else if([object isKindOfClass:[NSDictionary class]])
        [self enumerateDictionary:object];
    else if([object isKindOfClass:[NSString class]])
        NSLog(@"Name => %@\nChecked => %@\n", dictionary[@"Name"], dictionary[@"Checked"]);
  }
}

当您阅读字典时,只需将字典传递给此函数,它将显示所有值和相应的检查值。然后,您可以添加一些代码来选择特定的代码。

于 2013-09-04T22:09:56.403 回答