解析从 SignalR 接收到的数据时遇到问题

问题描述 投票:0回答:2

当尝试解析接收到的数据时,值为零,我可以在调试日志中看到接收到的数据,但无法解析数据。

我正在使用 https://github.com/moozzyk/SignalR-Client-Swift

   self.chatHubConnection!.on(method: "SendMessage", callback: { (payload: ArgumentExtractor?) in
            let response = try! payload?.getArgument(type: SignalR?.self)
            print("Response: \(response!)")
        })

型号

struct SignalR: Codable {
    let type: Int?
    let target: String?
    let arguments: [Argument]?
}

struct Argument: Codable {
    let id: ID


    enum CodingKeys: String, CodingKey {
        case id = "_id"
    }
}

struct ID: Codable {
    let timestamp, machine, pid, increment: Int
    let creationTime: Date
}
swift websocket signalr
2个回答
1
投票

使用

do try catch
这样你可以获得更好的错误而不是崩溃。

self.chatHubConnection!.on(method: "SendMessage", callback: { (payload: ArgumentExtractor?) in
        do{
            let response = try payload?.getArgument(type: SignalR?.self)
            print("Response: \(response!)")
        }catch{
            print(error)
        }
    })

0
投票

SDK(https://github.com/SwiftyJSON/SwiftyJSON.git) 或 Cocapods (SwiftyJSON)

import SwiftyJSON

self.chatHubConnection!.on(method: "ReceiveMessage", callback: { (payload: JSON) in
        print("Json :- \(payload)")
    })
© www.soinside.com 2019 - 2024. All rights reserved.