简体   繁体   English

Argo:类型不符合“可解码”协议

[英]Argo: Type does not conform to protocol 'Decodable'

I am just starting out with Argo to parse my json response into objects.我刚开始使用 Argo 将我的 json 响应解析为对象。 I having the following code (see below) but it keeps throwing up the following errors:我有以下代码(见下文),但它不断抛出以下错误:

Type 'Application' does not conform to protocol 'Decodable'类型“应用程序”不符合协议“可解码”

Cannot invoke 'curry' with an argument list of type '((applicationID: String, contact: String, state: String, jobTitle: String, area: String, pay: String) -> Application)'无法使用类型为 '((applicationID: String, contact: String, state: String, jobTitle: String, area: String, pay: String) -> Application)' 的参数列表调用 'curry'

import Foundation
import Argo
import Curry

struct Application {

    let applicationID: String
    let contact: String
    let state: String
    let jobTitle: String
    let area: String
    let pay: String
}

extension Application: Decodable {
    static func decode(j: JSON) -> Decoded<Application> {
        return curry(Application.init)
        <^> j <| "ApplicationID"
        <*> j <| "contact"
        <*> j <| "state" // Use ? for parsing optional values
        <*> j <| "jobTitle" // Custom types that also conform to Decodable just work
        <*> j <| "area" // Parse nested objects
        <*> j <| "pay" // parse arrays of objects
    }
}

I have extended application to be decodable so don't understand why I am getting this error.我已将应用程序扩展为可解码,所以不明白为什么会出现此错误。

Also I have tried adding the example from the Argo git hub page here: https://github.com/thoughtbot/Argo with struct type User .我还尝试在此处添加 Argo git 中心页面中的示例: https : //github.com/thoughtbot/Argo结构类型为User However this is throwing up the same error.然而,这引发了同样的错误。

I have used cocoa pods to install argo and curry.我使用可可豆荚安装 argo 和 curry。 I have also cleaned my project and restarted since installing them.我还清理了我的项目并在安装它们后重新启动。 However I am still getting these errors.但是我仍然收到这些错误。

Does anyone know why this might be happening?有谁知道为什么会发生这种情况?

I installed Argo and Curry using CocoaPods.我使用 CocoaPods 安装了 Argo 和 Curry。 Then tried the following code.然后尝试了以下代码。 It works fine without any problem.它工作正常,没有任何问题。

import UIKit

import Argo
import Curry

struct User {
    let id: Int
    let name: String
}

extension User: Decodable {
    static func decode(j: JSON) -> Decoded<User> {
        return curry(User.init)
            <^> j <| "id"
            <*> j <| "name"
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let stringJSON = "{\"id\":1, \"name\":\"Siva\"}"
        let data = stringJSON.dataUsingEncoding(NSUTF8StringEncoding)

        let json: AnyObject? = try? NSJSONSerialization.JSONObjectWithData(data!, options: [])

        if let j: AnyObject = json {
            let user: User? = decode(j)
            print("user - ", user)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUI-类型“服务”不符合协议“可解码” - SwiftUI - Type 'Service' does not conform to protocol 'Decodable' Swift 4.2:类型“ T”不符合协议“可解码” - Swift 4.2 : Type 'T' does not conform to protocol 'Decodable' 如果我在 swift 的结构中使用协议类型,我的结构不符合协议“可解码”/“可编码” - My structure does not conform to protocol 'Decodable' / 'Encodable' if I use protocol type in my structure in swift 类型“”不符合协议“可编码” - Type ' ' does not conform to protocol 'Encodable' 类型[String:String]不符合协议“ AnyObject” - type [String: String] does not conform to protocol 'AnyObject' 类型“ AreaData”不符合协议“可编码” - Type 'AreaData' does not conform to protocol 'Encodable' 致命错误:字典 <String, Any> 不符合Decodable,因为Any不符合Decodable - Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable 类型“字符串”不符合协议“ NSCopying”-数组swift json错误 - Type 'String' does not conform to protocol 'NSCopying' - Array swift json Error Swift,ObjectMapper:类型“用户”不符合协议“可映射” - Swift, ObjectMapper: Type 'User' does not conform to protocol 'Mappable' 为什么这个模型不符合 Decodable? (一个多态的 JSON 圣诞故事) - Why does this model not conform to Decodable? (a polymorphic JSON Christmas tale)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM