簡體   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