Object+Dictionary.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // Object+Dictionary.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2022/10/20.
  6. // Copyright © 2022 Fin. All rights reserved.
  7. //
  8. import Foundation
  9. import RealmSwift
  10. extension Object {
  11. func toDictionary() -> [String: AnyObject] {
  12. var dicProps = [String: AnyObject]()
  13. self.objectSchema.properties.forEach { property in
  14. if property.isArray {
  15. var arr: [[String: AnyObject]] = []
  16. for obj in self.dynamicList(property.name) {
  17. arr.append(obj.toDictionary())
  18. }
  19. dicProps[property.name] = arr as AnyObject
  20. } else if let value = self[property.name] as? Object {
  21. dicProps[property.name] = value.toDictionary() as AnyObject
  22. } else if let value = self[property.name] as? Date {
  23. dicProps[property.name] = Int64(value.timeIntervalSince1970) as AnyObject
  24. } else {
  25. let value = self[property.name]
  26. dicProps[property.name] = value as AnyObject
  27. }
  28. }
  29. return dicProps
  30. }
  31. }