// // Data+Cache.swift // SwiftBilibili // // Created by 罗文 on 2021/1/22. // Copyright © 2021年 罗文. All rights reserved. // import ObjectMapper import SwiftyJSON extension Data { func cacheObject(_ type: T.Type) -> T { let json = try! JSON(data: self) let data = json[RESULT_DATA].dictionaryObject return try! Mapper().map(JSON: data!) } func cacheArray(_ type: T.Type) -> [T] { let jsonArray = try! JSON(data: self).arrayObject let data = try! JSONSerialization.data(withJSONObject: jsonArray!, options: JSONSerialization.WritingOptions.prettyPrinted) let jsonString = String(data: data, encoding: String.Encoding.utf8)! return try! Mapper().mapArray(JSONString: jsonString) } }