【发布时间】:2014-01-01 18:05:54
【问题描述】:
我一直在寻找如何为下面的数组建模。我通过tide.tideSummary 访问这个数组 - 我已经尝试过自己建模具有 1 级数据的基本字典的示例,但这有嵌套数据。我正在研究看起来很棒的 JSONModel,但我认为它不能深入嵌套字典?
我知道如何获取单个对象,例如 data 并获取 pretty - 但我想显示几个部分并尝试通过以下方式显示它们:(我会在我的头文件中声明每个对象)。
self.date.pretty
self.data.type等
有这样做的想法吗?我认为循环遍历每个项目并添加到一个新对象将是矫枉过正,似乎太多的工作和处理来做我需要做的事情。
"tideSummary": [
{
"date": {
"pretty": "11:58 AM PST on December 19, 2013",
........
},
"utcdate": {
"pretty": "7:58 PM GMT on December 19, 2013",
........
},
"data": {
"height": "5.97 ft",
"type": "High Tide"
}
},
........,
........,
这是我目前的解决方案:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *eachTideSummary = [TideModel tideSingleton].tideSummary[indexPath.row];
NSDictionary *dataDic = [eachTideSummary objectForKey:@"data"];
NSDictionary *dateDic = [eachTideSummary objectForKey:@"date"];
NSLog(@"Pretty date: %@", [dateDic objectForKey:@"pretty"]);
NSLog(@"Data type: %@", [dataDic objectForKey:@"type"]);
cell.textLabel.text = [dataDic objectForKey:@"type"];
return cell;
}
想听听任何想法或建议吗?谢谢
【问题讨论】:
-
您可以使用KVC(提取单元格所需的对象后):
[eachTideSummary valueForKeyPath:@"data.height"] -
@DanShelly 谢谢,性能/最佳实践是“可以接受的”。我目前的解决方案就是这样做。
标签: ios objective-c arrays json dictionary