【问题标题】:I am try to fetch the data from sql server through WCF service我正在尝试通过 WCF 服务从 sql server 获取数据
【发布时间】:2018-10-30 06:28:13
【问题描述】:
import UIKit

public struct student: Decodable {
    let id:Int
    let name:String
}

class ViewController: UIViewController {

    @IBAction func btnshow(_ sender: Any) {

        let link = "http://10.211.55.3/WcfService1/Service1.svc/GetData"
        guard let url = URL(string: link)else{
            print("error during connection")
            return
        }
        URLSession.shared.dataTask(with: url) { (data, response, error) in

            guard let data = data
                else{                    print("there is no data")
                return
            }
            do{
                let students = try JSONDecoder().decode(student.self, from: data)
                print(students)


            } catch{
                print("conversion error")
                print(error)
            }
        }.resume()            
    }
}

keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "否 与键 CodingKeys 关联的值(stringValue: \"id\", intValue: nil) (\"id\").", 底层错误: nil))

【问题讨论】:

    标签: ios swift nsurlsession codable


    【解决方案1】:

    嗯,打印出来的错误很容易解释。您获得的数据未按照您期望的方式进行格式化,并且缺少某些键(id 键),因此JSONDecoder() 无法对其进行解码。

    在尝试解码之前确保您得到的响应格式正确。

    编辑:所以在看到您的评论后,您的模型与您收到的 JSON 不匹配,应该是这样的:

    import UIKit
    
    public struct JsonStudent: Decodable {
        let students: [Student]
    
        enum CodingKeys: String, CodingKey {
            case students = "GetDataResult"
        }
    }
    
    public struct Student: Decodable {
        let id: Int
        let name: String
    }
    
    class ViewController: UIViewController {
    
        @IBAction func btnshow(_ sender: Any) {
            let link = "http://10.211.55.3/WcfService1/Service1.svc/GetData"
    
        guard let url = URL(string: link) else {
                print("error during connection")
                return
        }
    
        URLSession.shared.dataTask(with: url) { (data, response, error) in
                guard let data = data
                    else {                    print("there is no data")
                    return
                }
                do {
                    let students = try JSONDecoder().decode(JsonStudent.self, from: data)
                    print(students)
                } catch {
                    print("conversion error")
                    print(error)
                }
            }.resume()            
        }
    }
    

    【讨论】:

    • 所以它确认您在回复中没有收到idname
    • 这取决于您的基础架构。我不知道什么返回您的服务器。尝试记录和/或检查您的回复。
    • {"GetDataResult":[{"id":1,"name":"saqib"},{"id":2,"name":"waqas"},{"id" :3,"name":"ali "}]}
    • 函数返回这三个json对象,但在应用中它不起作用。
    • 对不起,我又编辑了(忘记修改JsonDecoder)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多