保存和检索多个词典到plist文件

时间:2011-01-11 08:58:35

标签: objective-c nsmutabledictionary uitableview

因为我是iPhone编程的新手,并尝试对其进行排序,我有一个问题,我正在尝试以下列格式将字典保存到plist ...

-Root
    -StateName (Dictionary)
        - TitleOfLocation (Dictionary)
            -Address (String)
            -City (String)
            -State (String)

也许我完全不在这里或逻辑需要改变,但最后我需要它填充在按州名排序的UITableView中。

目前我可以将数据保存到单个字典并将其写入PLST并将其拉入UITableView(基础知识,是的,我知道),但正如我所说,我只是在保存嵌套字典时遇到问题。

有人能指导我正确的方向吗?感谢。

1 个答案:

答案 0 :(得分:0)

您可以创建嵌套数组而不是嵌套字典。并创建另一个包含状态名称的数组。

- Root (array) 
 - Texas (array) 
  - Location1 (dictionary)
   - TitleOfLocation (string)
   - Address (string)
   - ...
 - Florida (array)
  - Location1 (dictionary)
   - ...
  - Location2 (dictionary)
   - ...
// and
- StateNames (array)
 - Texas (string)
 - Florida (string)

为方便起见,您可以将这两个数组放入单个NSDictionary中。我的例子中没有这样做 因为NSArray,NSDictionary和NSString都确认了NSCoding协议,你可以使用writeToFile将这个字典写入磁盘:atomically:

我为你写了一些示例代码,因为我认为解决方案非常简单,但很难用文字描述。

#define kKeyDescription @"kKeyDescription"
#define kKeyAddress     @"kKeyAddress"
#define kKeyCity        @"kKeyCity"
#define kKeyState       @"kKeyState"

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *root = [NSMutableArray array];
    NSMutableArray *sectionTitle = [NSMutableArray array];

    NSMutableArray *florida = [NSMutableArray array];
    [root addObject:florida];
    [sectionTitle addObject:@"Florida"];

    NSMutableArray *texas = [NSMutableArray array];
    [root addObject:texas];
    [sectionTitle addObject:@"Texas"];

    NSDictionary *location1 = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"My favorite pizza place", kKeyDescription,
                               @"5106 Grace Drive", kKeyAddress,
                               @"Miami", kKeyCity,
                               @"Florida", kKeyState,
                               nil];
    [florida addObject:location1];

    NSDictionary *location2 = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"Home", kKeyDescription,
                               @"1234 Foobar Street", kKeyAddress,
                               @"Fort Lauderdale", kKeyCity,
                               @"Florida", kKeyState,
                               nil];
    [florida addObject:location2];

    NSDictionary *location3 = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"Franks Workplace", kKeyDescription,
                               @"9101 Baz Avenue", kKeyAddress,
                               @"Houston", kKeyCity,
                               @"Texas", kKeyState,
                               nil];
    [texas addObject:location3];

    data = [root retain];
    sectionData = [sectionTitle retain];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.data count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.sectionData objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.data objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"MyCellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
    }
    NSDictionary *object = [[self.data objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    //                      < This is the array for the selected state >
    cell.textLabel.text = [object valueForKey:kKeyDescription];
    cell.detailTextLabel.text = [object valueForKey:kKeyAddress];
    return cell;
}
相关问题