以下代码实现了解析单个模型和数组,从字典或字典数组中解析到单个模型或者数组,仅供参考。
/*** 解析数据** @param data 要解析的数据** @return 解析后的数据**/public static func parse<T: Decodable>(data: [String: Any]?) -> T? {if data == nil {return nil}do {let jsonData = try JSONSerialization.data(withJSONObject: data!)let decoder = JSONDecoder()let model = try decoder.decode(T.self, from: jsonData)return model} catch {print(error)}return nil}/// 解析数组数据/// - Parameter data: 要解析的数据/// - Returns: 结果public static func parseArray<T: Codable>(data: Any?) -> [T] {let dataArray = data as? [[String: Any]]var list = [T]()if dataArray == nil {return list}for d in dataArray! {let m: T? = ParseTool.parse(data: d)if m != nil {list.append(m!)}}return list}