Dictionary+Response.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // Dictionary+Response.swift
  3. // MiMoLive
  4. //
  5. // Created by OneeChan on 2025/9/18.
  6. //
  7. import Foundation
  8. extension Dictionary<AnyHashable, Any> {
  9. var data: [AnyHashable: Any]? {
  10. self["data"] as? [AnyHashable: Any]
  11. }
  12. var code: Int {
  13. intValue(for: "code")
  14. }
  15. var isCodeSuccess: Bool {
  16. let code = code
  17. return code == 0 || code == 200
  18. }
  19. func intValue(for key: String, _ defaultValue: Int = 0) -> Int {
  20. guard let value = self[key] else { return defaultValue }
  21. let intValue: Int? = if let intValue = value as? Int {
  22. intValue
  23. } else if let strValue = value as? String {
  24. Int(strValue)
  25. } else {
  26. nil
  27. }
  28. return intValue ?? defaultValue
  29. }
  30. func showNetError() {
  31. guard let err = MODataManager.objectOrNil(forKey: "msg", from: self) as? String else { return }
  32. if err.isEmpty {
  33. MBProgressHUD.showTipMessage(inWindow: NSLocalizedString("mimo_common_data_error", comment: ""))
  34. } else {
  35. MBProgressHUD.showTipMessage(inWindow: err)
  36. }
  37. }
  38. }