[英]NSArray element failed to match the Swift Array Element type Expected JSONPaymentCard but found __NSDictionaryI
I am using JSONModel to cast values from server:我正在使用 JSONModel 从服务器转换值:
@interface PaymentCardsResponse: JSONModel
@property (strong, nonatomic) NSArray<JSONPaymentCard *> *userCards;
@end
But when later I try to access this但是当后来我尝试访问这个
response.userCards.forEach { card in } //here is an error
I have an error:我有一个错误:
Precondition failed: NSArray element failed to match the Swift Array Element type Expected JSONPaymentCard but found __NSDictionaryI
前提条件失败:NSArray 元素无法匹配 Swift 数组元素类型预期 JSONPaymentCard 但找到 __NSDictionaryI
Why do I have it there?为什么我有它? What am I missing?
我错过了什么?
In the README file of JSONModel, there is a note saying:在 JSONModel 的README文件中,有一条注释说:
@interface OrderModel: JSONModel @property (nonatomic) NSInteger orderId; @property (nonatomic) float totalPrice; @property (nonatomic) NSArray <ProductModel> *products; @end
Note: the angle brackets after NSArray contain a protocol.
注意:NSArray 后面的尖括号包含一个协议。 This is not the same as the Objective-C generics system.
这与 Objective-C generics 系统不同。 They are not mutually exclusive, but for JSONModel to work, the protocol must be in place.
它们不是相互排斥的,但要使 JSONModel 工作,协议必须到位。
JSONModel uses the type in the <>
to determine the specific type of array to deserialise, and it is specifically noted that you can't replace this with Objective-C generics system ("not the same",): so if you did: JSONModel 使用
<>
中的类型来确定要反序列化的数组的具体类型,并特别注意不能将其替换为 Objective-C generics 系统(“不一样”,):所以如果你这样做了:
@property (strong, nonatomic) NSArray<JSONPaymentCard *> *userCards;
You are not telling JSONModel what model type the array should contain, so it just dumbly deserialises NSDictionary
s.你没有告诉 JSONModel 数组应该包含什么 model 类型,所以它只是愚蠢地反序列
NSDictionary
s。 You can do both Objective-C generics, and tell JSONModel the type of the array, as demonstrated by the next code snippet in the README.您可以同时执行 Objective-C generics,并告诉 JSONModel 数组的类型,如 README 中的下一个代码片段所示。
@property (strong, nonatomic) NSArray<JSONPaymentCard *> <JSONPaymentCard> *userCards;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.