【发布时间】:2017-12-15 22:37:10
【问题描述】:
我正在使用 RestKit 进行 API 调用,但我的一个属性没有正确映射。
说我有:
// Animal.h
@interface Animal: NSObject
@property (nonatomic, strong) NSArray<Fruit*> favoriteFruits;
@end
实施:
// Animal.h
// This class conforms to NSCoding and correctly implements encodeWithCoder and initWithCoder, no problem
我还有一个 Fruit 类,里面有类似的 NSCoding 东西:
// Fruit.h
@interface Fruit: NSObject
@property (nonatomic, strong) NSString* name;
@end
以下是我设置映射的方式:
[animalMapping addAttributeMappingsFromDictionary:@{
@"fruits": @"favoriteFruits"
}];
当我通过 RestKit 发出 GET 请求时,我得到的 JSON 是这样的:
{
"fruits": [
{
"name": "banana"
},
{
"name: "apple"
}
]
}
请求已成功发出,但是当我检查映射结果时,Animal 类中的 favoriteFruits 数组中的元素不像我预期的那样属于 Fruit 类型。它们的类型为NSDictionary。 NSDictionary 上的值都是正确的...我只需要将它们自动转换为 Fruit 对象...
不确定这是否相关,但是当我在调试器中尝试时:
(lldb) po ((Fruit*)myAnimal.favoriteFruits[0]).name
我明白了:
错误:执行被中断,原因:试图取消引用无效的 ObjC 对象或向其发送无法识别的选择器。 进程已经返回到表达式求值前的状态。
感谢您的帮助。
【问题讨论】:
标签: objective-c afnetworking restkit nscoding