gpt4 book ai didi

ios - Reskit - 映射到数组

转载 作者:行者123 更新时间:2023-11-29 13:21:28 26 4
gpt4 key购买 nike

我正在尝试运行一些单元测试来测试我与 RestKit v0.20 的映射,但是我收到一个错误消息,指出我的目标对象为 nil。我已经追踪到映射失败的事实,因为 sourceType 是一个 NSArray 而我的 destinationType 是一个 NSNumber。我认为这是因为我的映射键路径不正确。我正在尝试将 songCard JSON 映射到我的对象。我在下面包含了我的 JSON 和映射测试。如果有人可以帮助我设置正确的键路径,那就太好了。

{"status" : 2000,
"content" : {
"cardList" : [
{
"songCard" : {
"likes" : 2,
"dislikes" : 3
}
}
]
},
"message" : "OK"
}

单元测试类

- (RKObjectMapping *)songMetadataMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[SongMetadata class]];
[mapping addAttributeMappingsFromDictionary:@{
@"content.cardList.songCard.likes": @"likes"
}];

return mapping;
}

- (void)testSongMetadataMapping
{
NSString *parsedJSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"songMetadata.json"];
RKMappingTest *test = [RKMappingTest testForMapping:[self songMetadataMapping] sourceObject:parsedJSON destinationObject:nil];
[test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"content.cardList.songCard.likes" destinationKeyPath:@"likes" value:@"2"]];

STAssertTrue([test evaluate], @"Mappings failed");

}

更新进一步调试后,我发现我的 JSON 字符串中的值 2 被评估为 NSArray,而这应该被评估为 NSNumber。作为快速测试,我删除了我的 JSON 中的 [ ] 并且值 2 被正确评估为 NSNumber。这并不能解决我的问题,因为我需要将我的 JSON 标识为一组 songCard 对象

最佳答案

正如您所注意到的,您不能使用您在播放数组时指定的键路径。我可以想到两个选项 - 第一个是远景,但关键路径 content.cardList[0].songCard.likes 是否有效?

否则,考虑使用方法:

+ (instancetype)expectationWithSourceKeyPath:(NSString *)sourceKeyPath 
destinationKeyPath:(NSString *)destinationKeyPath
evaluationBlock:(RKMappingTestExpectationEvaluationBlock)evaluationBlock;

使用 keypath content.cardList 并提供评估 block 1) 检查映射是否是包含单个对象的数组。然后,您可以检查该对象是否包含一个 songCard 对象,并且该对象的 likes 值为 2。

关于ios - Reskit - 映射到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14275631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com