简体   繁体   English

Xcode Swift Argument 类型不符合预期类型“解码器”

[英]Xcode Swift Argument type does not conform to expected type 'Decoder'

I have a little problem with "Decoder" I use URLSession and Xcode 11.3.我使用 URLSession 和 Xcode 11.3 的“解码器”有一个小问题。 This is Error message Argument type 'Data' does not conform to expected type 'Decoder' Check this out这是错误消息参数类型“数据”不符合预期类型“解码器”检查这一点

JSON JSON

[
    {
        "id": 1,
        "titel": "Telefonständer",
        "ersteller": "von Mike-vom-Mars",
        "urlLink": "https://www.thingiverse.com/thing:3513800",
        "mehr": "Faltbarer Tablet- und Handyhalter, der fünf verschiedene Kipppositionen ermöglicht. Dieser Halter benötigt keine Schrauben, einfach ausdrucken und zusammenstecken. Sehr stabil, platzsparend und tragbar."
    },
    {
        "id": 2,
        "titel": "Tool Caddy",
        "ersteller": "von Tom Link",
        "urlLink": "https://www.thingiverse.com/thing:2849473",
        "mehr": "Ein einfacher Werkzeugwagen für den 3D-Druck. Ca. 4,75 x 3,125 x 1,3 Zoll. Hält Messer, Pinzetten, Rasierklingenschaber, Klebestift, SD-Kartenadapter und ein Abfallfach."
    },
    {
        "id": 3,
        "titel": "Honey, I'm Home!",
        "ersteller": "von Luz Cabrera und Malorie Pangilinan",
        "urlLink": "https://www.behance.net/gallery/16925735/Honey-Im-Home",
        "mehr": "Honig-Schlüsselanhänger. Wenn Sie direkt auf die endgültige Farbe drucken, empfehle ich, 100% für das Rahmenteil und 105% für die kleinen Teile zu verwenden, damit sie besser in die Löcher passen. Wenn Sie jedoch die Teile bemalen, empfehle ich 100% beide."
    }
]

func getRequest(completion: @escaping ([Model]?) -> Void) {
    let url = URL(string: "http://localhost" + ":" + "8080" + "/" + "printdata")!

    let task = URLSession.shared.dataTask(with: url) {
        (data, response, error) in
        let jsonDecoder = JSONDecoder()
        if let data = data,
            var categories = try? jsonDecoder.decode([Model].self, from: data) {
            completion(categories)

            for reply in data{
                print("Show: \(reply)")
                categories.append(Model(from: data))

            }
        } else {
            print("ERROR")
            completion(nil)
        }
        //debugPrint(String(data: data!, encoding: .utf8))
    }
    task.resume()
}

And my Model:还有我的模型:

struct Model: Decodable{
    var id: Int
    var titel: String
    var ersteller: String
    var mehr: String
    var urlLink: String?

    init(id: Int, titel: String, ersteller: String, mehr: String, urlLink: String) {
        self.id = id
        self.titel = titel
        self.ersteller = ersteller
        self.mehr = mehr
        self.urlLink = urlLink
    }

}

Maybe the JSON you get is not an array of Model?也许你得到的 JSON 不是模型数组?

You can print out the response string to check if you got the right type.您可以打印出响应字符串以检查您的类型是否正确。

it seems your responsedata doesn't match the items in struct.看来您的 responsedata 与结构中的项目不匹配。 The comment only is a response评论只是回应

Optional( { URL: localhost:8080/printdata } { Status Code: 200, Headers { Connection = ( "keep-alive" ); "Content-Length" = ( 1068 ); "Content-Type" = ( "application/json; charset=utf-8" ); Date = ( "Tue, 14 Jan 2020 09:00:41 GMT" ); } ckquote可选( { URL: localhost:8080/printdata } { Status Code: 200, Headers { Connection = ( "keep-alive" ); "Content-Length" = ( 1068 ); "Content-Type" = ( "application/json ; charset=utf-8" ); Date = ( "Tue, 14 Jan 2020 09:00:41 GMT" ); } ckquote

. . Kindly convert the data you get to a JSON/String and post it here.请将您获得的数据转换为 JSON/String 并在此处发布。 This would help us to answer这将有助于我们回答

The loop for reply in data makes no sense, the data is already decoded completely, delete it, this fixes the error. for reply in data循环没有意义,数据已经完全解码,删除它,这修复了错误。

And don't try?try? , always catch errors. ,总是捕捉错误。

func getRequest(completion: @escaping ([Model]?) -> Void) {
    let url = URL(string: "http://localhost" + ":" + "8080" + "/" + "printdata")!

    let task = URLSession.shared.dataTask(with: url) {
        (data, response, error) in
        if let error = error {
           print("Connection error", error)
           completion(nil)
        } else {
           do {
               let jsonDecoder = JSONDecoder()
               let categories = try jsonDecoder.decode([Model].self, from: data!)
               completion(categories)
           } catch {
               print("Decoding error", error)
               completion(nil)
           }
        }
        //debugPrint(String(data: data!, encoding: .utf8))
    }
    task.resume()
}

The init method in Model is not needed and the struct members can probably be constants不需要Model的 init 方法,结构成员可能是常量

struct Model: Decodable {
    let id: Int
    let titel: String
    let ersteller: String
    let mehr: String
    let urlLink: String?
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 Swift 4/Xcode 10 的 Facebook SDK:参数类型“SDKLoggingBehavior?” 不符合预期的类型“序列” - Facebook SDK with Swift 4/Xcode 10: Argument type 'SDKLoggingBehavior?' does not conform to expected type 'Sequence' 参数类型与预期类型不符 - Argument type does not conform to expected type 参数类型不符合预期的类型 MKAnnotation - Argument type does not conform to expected type MKAnnotation 参数类型&#39;customClass.Type&#39;不符合预期类型&#39;NSItemProviderWriting&#39; - Argument type 'customClass.Type' does not conform to expected type 'NSItemProviderWriting' 参数类型&#39;范围 <Int> &#39;不符合预期类型&#39;序列&#39;Swift3 - Argument type 'Range<Int>' does not conform to expected type 'Sequence' Swift3 iOS SWIFT - 文件存档 - 参数类型“[String?]”:不符合预期类型&#39;AnyObject&#39; - iOS SWIFT - File Archive - Argument Type “[String?]” : does not conform to expected type'AnyObject' 参数类型“字符串?” 不符合预期的类型“StringProtocol” - Argument type 'String?' does not conform to expected type 'StringProtocol' 参数类型'[String?]'不符合预期类型'AnyObject' - Argument type '[String?]' does not conform to expected type 'AnyObject' 参数类型&#39;SecretSpec&#39;不符合预期类型&#39;序列&#39; - Argument type 'SecretSpec' does not conform to expected type 'Sequence' 参数类型“ AnyObject”不符合预期的类型NSCopying - Argument Type 'AnyObject' does not conform to expected type NSCopying
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM