如何修改 iPhone plist 以添加另一个级别并在代码中访问数据

Posted

技术标签:

【中文标题】如何修改 iPhone plist 以添加另一个级别并在代码中访问数据【英文标题】:How to modify iPhone plist to add another level and access data in code 【发布时间】:2009-12-22 19:42:39 【问题描述】:

我有一个带有按字母索引的条目列表的 plist。我现在想添加一个更进一步的级别,以便每个条目都有一个定义文本(就像一本小字典)。我需要一些关于如何修改我的 plist 和代码以适应它的建议。现有代码如下 - 它来自 Apress 书,但我似乎无法将其修改为另一个级别。

NSString *path = [[NSBundle mainBundle] pathForResource:@"sortedglossary" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
self.names = dict;
[dict release];

NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.keys = array;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
NSLog(@"numberOfSectionInTable start");
return [keys count];



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
NSLog(@"numberOfRowsInSection start");
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
return [nameSection count];


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SectionsTableIdentifier] autorelease];


cell.text = [nameSection objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
NSLog(@"titleForHeader");
NSString *key = [keys objectAtIndex:section];
return key;


-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
return keys;

【问题讨论】:

【参考方案1】:

有很多方法可以做到这一点。通常我会在 plist 中创建另一个根节点的子节点来进行定义。

因此,您的 plist 将包含您的条目数组,并且将包含另一个字典。字典包含定义文本,由第一个数组中的值作为键。这样,您可以在需要时查找定义。

所以结构大概是这样的:

Root
  Entries(NSArray)
    0 => entryA(NSString)
    1 => entryB(NSString)
    ...
    x => entryX(NSString)
  Definitions(NSDictionary)
    entryA(NSString) => definitionA(NSString)
    entryB(NSString) => definitionB(NSString)
    ...
    entryX(NSString) => definitionX(NSString)

【讨论】:

谢谢。字母表中的每个字母都会有多个条目(如迷你字典)。所以它看起来像: Root ?? A (Array) => Item 0 (Dictionary) => AFirstWord (Array) => AFirstdefinition (String), Item 1 (Dictionary) => ASecondWord (Array) => ASeconddefinition (String) B (Array) => Item 0 ( Dictionary) => BFirstWord (Array) => BFirstdefinition (String), Item 1 (Dictionary) => BSecondWord (Array) => BSeconddefinition (String) 嗯,没有真正的“正确”或“错误”的方式来做到这一点。一切都取决于您的程序的需求。听起来您的需求是对这些条目进行排序条目和定义。对于我所描述的方法,每个字母表有多个条目不应该是问题。您提出的解决方案可能会起作用,但它似乎很复杂,并且嵌套数组/字典对内存不是非常友好。不过,这一切都取决于您的特定应用程序的需求。 感谢您在这里的帮助和时间。关键是我已经尽我所能,并且需要一些建议,当我需要多个级别时,作为 plist 的结构的最佳方法是什么,就像这里一样,坦率地说,我迷路了。跨度>

以上是关于如何修改 iPhone plist 以添加另一个级别并在代码中访问数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iphone 中以编程方式创建 PLIST 文件

如何显示最初从 plist iphone 中隐藏的状态栏

如何以编程方式将字符串和键添加到 iPhone 中的字典?

在文档目录中找不到 plist 文件?如何以编程方式修改 plist?

读取 plist 文件以填充表格时出错

如何使存储在文档目录中的 plist 在 iPhone 和 iPad 之间为通用应用程序同步?