[英]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.