TSBaseModel.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // TSBaseModel.swift
  3. // TSLiveWallpaper
  4. //
  5. // Created by 100Years on 2024/12/22.
  6. //
  7. import ObjectMapper
  8. open class TSBaseModel: NSObject, Mappable, NSCopying {
  9. public static func model(data: Data) -> TSBaseModel? {
  10. do {
  11. let object = try JSONSerialization.jsonObject(with: data)
  12. if let dict = object as? [String: AnyHashable] {
  13. return Self.init(json: dict)
  14. }
  15. } catch _ {
  16. return nil
  17. }
  18. return nil
  19. }
  20. open var data: Data? {
  21. let json = toJSON()
  22. do {
  23. let data = try JSONSerialization.data(withJSONObject: json)
  24. return data
  25. } catch _ {
  26. return nil
  27. }
  28. }
  29. required convenience public init?(map: ObjectMapper.Map) {
  30. self.init()
  31. mapping(map: map)
  32. }
  33. required convenience public init?(json: [String: Any]) {
  34. self.init(JSON: json)
  35. }
  36. open func mapping(map: ObjectMapper.Map) {}
  37. open func copy(with zone: NSZone? = nil) -> Any {
  38. let json = toJSON()
  39. return Self.init(JSON: json) ?? Self.init(JSON: [:])!
  40. }
  41. // deinit {
  42. // debugPrint("♻️♻️♻️ TGRootViewController -> \(type(of: self)) deinit ♻️♻️♻️")
  43. // }
  44. }