jsdw_ios/QuickLocation/Core/Extension/ObjectMapper+Extension.swift

37 lines
888 B
Swift

//
// ObjectMapper+Extension.swift
// DLSDK
//
// Created by osell on 2023/4/7.
//
import ObjectMapper
public extension Mappable {
init?(data: Data, context: MapContext? = nil) {
guard let dataStr = String(data: data, encoding: .utf8) else {
return nil
}
self.init(JSONString: dataStr, context: context)
}
func toData(prettyPrint: Bool = false) -> Data? {
toJSONString(prettyPrint: prettyPrint)?.data(using: .utf8)
}
}
public extension Array where Element: BaseMappable {
init?(data: Data, context: MapContext? = nil) {
guard let dataStr = String(data: data, encoding: .utf8) else {
return nil
}
self.init(JSONString: dataStr)
}
func toData(prettyPrint: Bool = false) -> Data? {
toJSONString(prettyPrint: prettyPrint)?.data(using: .utf8)
}
}