使用嵌套对象和键建模数组

时间:2014-01-01 18:05:55

标签: ios objective-c arrays json dictionary

我一直在寻找如何建模下面的数组。我通过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;
}

愿意听到任何想法或指示吗?感谢

1 个答案:

答案 0 :(得分:2)

通过github查看Mantle Framework。它易于使用且非常强大。

更新

#import <Specta.h>
#import <Expecta.h>
#import <Mantle.h>

#pragma mark GIInterfaces

@interface GIDate : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSString *pretty;
@end

@interface GIUtcDate : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSString *pretty;
@end

@interface GIData : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSString *height;
@property (nonatomic, strong) NSString *type;
@end

@interface GITide : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) GIDate *date;
@property (nonatomic, strong) GIUtcDate *utcDate;
@property (nonatomic, strong) GIData *data;
@end

@interface GITideSummary : MTLModel <MTLJSONSerializing>
@property (nonatomic, strong) NSArray *tides;
@end

#pragma mark - GIImplementation

@implementation GIDate

+ (NSDictionary*)JSONKeyPathsByPropertyKey{
    return @{@"pretty": @"pretty"};
}

@end

@implementation GIUtcDate

+ (NSDictionary*)JSONKeyPathsByPropertyKey{
    return @{@"pretty": @"pretty"};
}

@end

@implementation GIData

+ (NSDictionary*)JSONKeyPathsByPropertyKey{
    return @{@"height": @"height",
             @"type" : @"type"};
}

@end

@implementation GITide

+ (NSDictionary*)JSONKeyPathsByPropertyKey{
    return @{@"date": @"date",
             @"utcDate" : @"utcdate",
             @"data": @"data"};
}

+ (NSValueTransformer*)dateJSONTransformer{
    return [MTLValueTransformer mtl_JSONDictionaryTransformerWithModelClass:[GIDate class]];
}

+ (NSValueTransformer*)utcDateJSONTransformer{
    return [MTLValueTransformer mtl_JSONDictionaryTransformerWithModelClass:[GIUtcDate class]];
}

+ (NSValueTransformer*)dataJSONTransformer{
    return [MTLValueTransformer mtl_JSONDictionaryTransformerWithModelClass:[GIData class]];
}

@end


@implementation GITideSummary

+ (NSDictionary*)JSONKeyPathsByPropertyKey{
    return @{@"tides" : @"tideSummary"};
}

+ (NSValueTransformer*)tidesJSONTransformer{
    return [MTLValueTransformer mtl_JSONArrayTransformerWithModelClass:[GITide class]];
}

@end

SpecBegin(MantleTestClass)

it(@"should initialize GITideSummary from JSON", ^{
    NSDictionary *values =
    @{
      @"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"
                                    }
                            }]
      };

    NSError *error = nil;
    GITideSummary *summary = [MTLJSONAdapter modelOfClass:[GITideSummary class]
                                       fromJSONDictionary:values
                                                    error:&error];

    expect(summary).to.beKindOf([GITideSummary class]);
    expect(summary.tides.count).to.equal(1);
    GITide *tide = summary.tides[0];
    expect(tide).to.beKindOf([GITide class]);
    expect(tide.data).to.beKindOf([GIData class]);
    expect(tide.data.height).to.equal(@"5.97 ft");
});

SpecEnd