当尝试解析接收到的数据时,值为零,我可以在调试日志中看到接收到的数据,但无法解析数据。
我正在使用 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
}
使用
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)
}
})
SDK(https://github.com/SwiftyJSON/SwiftyJSON.git) 或 Cocapods (SwiftyJSON)
import SwiftyJSON
self.chatHubConnection!.on(method: "ReceiveMessage", callback: { (payload: JSON) in
print("Json :- \(payload)")
})