Swift-利用Mirror创建结构体字典(JSON)
1 | struct MyStruct: Codable { |
You can do this without the CodingKeys (if the table attribute names on server side are equal to your struct property names). In that case just use the ‘name’ from mirror.children.
If you need CodingKeys don’t forget to add the CaseIterable protocol. That makes it possible to use the allCases variable.
Be careful with nested structs: E.g. if you have a property with a custom struct as type, you need to convert that to a dictionary too. You can do this in the for loop.
The Array extension is required if you want to create an array of MyStruct dictionaries.
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 X Mεl0n | 随手记!