简体   繁体   中英

Argo: Type does not conform to protocol 'Decodable'

I am just starting out with Argo to parse my json response into objects. 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)'

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 . However this is throwing up the same error.

I have used cocoa pods to install argo and 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. 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)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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