gpt4 book ai didi

swift - 从 JSONDecoder.decode() 的字符串中动态获取类类型

转载 作者:可可西里 更新时间:2023-11-01 02:01:56 29 4
gpt4 key购买 nike

我想解码 websocket“通知”的 json 响应,其中通知类型在 json 响应中。

JSON 示例:

{
"jsonrpc": "2.0",
"method": "Application.OnVolumeChanged",
"params": {
"data": {
"muted": false,
"volume": 88.6131134033203125
},
"sender": "xbmc"
}
}

这是我目前拥有的:

func notificationMessage(text: String) {
do {
if let jsonData = text.data(using: .utf8),
let json = try JSONSerialization.jsonObject(with: jsonData) as? [String: Any],
let method = json["method"] as? String,
let methodName = method.split(separator: ".").last?.description {

let decoder = JSONDecoder()
let object = try decoder.decode(OnVolumeChanged.self, from: jsonData)

print(object)
}
} catch {
print("Error deserializing JSON: \(error)")
}
}

现在我想用 methodName 而不是 OnVolumeChanged.self。但我不想在 methodName 上做一个巨大的 switch case,因为我可以获得数百种不同的方法我试过 NSClassFromString(methodName) 但这给了我 AnyClass? 这不是具体类型。

有没有办法从字符串中获取类类型?

最佳答案

I have faced the same problem and here is my solution. You can add methods to Mapper dictionary any time you want.

//1

let Mapper: [String: Any] = ["OnVolumeChanged"  : OnVolumeChanged.self]

//2

func notificationMessage(text: String) {
do {
if let jsonData = text.data(using: .utf8),
let json = try JSONSerialization.jsonObject(with: jsonData) as? [String: Any],
let method = json["method"] as? String,
let methodName = method.split(separator: ".").last?.description {

let className = Mapper[methodName] as! Codable.Type
let object = try className.init(jsonData: jsonData)


print(object)
}
} catch {
print("Error deserializing JSON: \(error)")
}
}

//3

extension Decodable {
init(jsonData: Data) throws {
self = try JSONDecoder().decode(Self.self, from: jsonData)
}
}

关于swift - 从 JSONDecoder.decode() 的字符串中动态获取类类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45660912/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com