gpt4 book ai didi

json - Argo:类型不符合协议(protocol) 'Decodable'

转载 作者:行者123 更新时间:2023-11-30 13:24:16 25 4
gpt4 key购买 nike

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

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
}
}

我已将应用程序扩展为可解码,因此不明白为什么会收到此错误。

我还尝试从 Argo git hub 页面添加示例:https://github.com/thoughtbot/Argo具有结构类型User。然而,这引发了同样的错误。

我使用 cocoa pod 来安装 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)
}
}
}

关于json - Argo:类型不符合协议(protocol) 'Decodable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37409213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com