简体   繁体   English

NSArray 元素与 Swift 数组元素类型不匹配 预期为 JSONPaymentCard 但找到了 __NSDictionaryI

[英]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.

相关问题 NSArray元素未能与Swift Array元素类型的Expected NSMutableArray相匹配,但找到了__NSDictionaryI: - NSArray element failed to match the Swift Array Element type Expected NSMutableArray but found __NSDictionaryI: 致命错误:NSArray元素无法匹配Swift数组元素类型 - fatal error: NSArray element failed to match the Swift Array Element type AlamofireJson / EVReflection-NSArray元素无法与以下中的Swift Array Element类型匹配 - AlamofireJson/EVReflection - NSArray element failed to match the Swift Array Element type in NSArray元素无法与Swift数组元素类型匹配 - NSArray element failed to match the Swift Array Element type PFObject子类化:NSArray元素与Swift Array元素类型不匹配 - PFObject Subclassing: NSArray element failed to match the Swift Array Element type NSArray元素的原因无法与Swift数组元素类型匹配 - reason of NSArray element failed to match the Swift Array Element type 使用数组指针和swift 1.2进行PFSubclassing - 致命错误:NSArray元素无法匹配Swift数组元素类型 - PFSubclassing with array pointer and swift 1.2 - fatal error: NSArray element failed to match the Swift Array Element type FetchRequest - NSArray元素无法匹配Swift数组元素类型 - Swift 2.0 - FetchRequest - NSArray element failed to match the Swift Array Element type - Swift 2.0 Firebase iOS Swift致命错误:NSArray元素与Swift Array元素类型不匹配 - Firebase iOS Swift fatal error: NSArray element failed to match the Swift Array Element type Swift 2.2-NSArray元素无法匹配TableViewController中单元格的Swift Array元素类型 - Swift 2.2 - NSArray element failed to match Swift Array Element Type for cells in a TableViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM