繁体   English   中英

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

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

我刚开始使用 Argo 将我的 json 响应解析为对象。 我有以下代码(见下文),但它不断抛出以下错误:

类型“应用程序”不符合协议“可解码”

无法使用类型为 '((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
    }
}

我已将应用程序扩展为可解码,所以不明白为什么会出现此错误。

我还尝试在此处添加 Argo git 中心页面中的示例: https : //github.com/thoughtbot/Argo结构类型为User 然而,这引发了同样的错误。

我使用可可豆荚安装 argo 和 curry。 我还清理了我的项目并在安装它们后重新启动。 但是我仍然收到这些错误。

有谁知道为什么会发生这种情况?

我使用 CocoaPods 安装了 Argo 和 Curry。 然后尝试了以下代码。 它工作正常,没有任何问题。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM