Sin Descripción

UserDefaultsExt.swift 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // UserDefaultsExt.swift
  3. // ExtensionKit
  4. //
  5. // Created by FFIB on 2017/9/22.
  6. // Copyright © 2017年 FFIB. All rights reserved.
  7. //
  8. import Foundation
  9. public protocol BoolUserDefaultable {
  10. associatedtype BoolDefaultKey: RawRepresentable
  11. }
  12. extension BoolUserDefaultable where BoolDefaultKey.RawValue == String {
  13. public static func set(_ value: Bool, forKey key: BoolDefaultKey) {
  14. let key = key.rawValue
  15. UserDefaults.standard.set(value, forKey: key)
  16. }
  17. public static func bool(forKey key: BoolDefaultKey) -> Bool {
  18. let key = key.rawValue
  19. return UserDefaults.standard.bool(forKey: key)
  20. }
  21. }
  22. public protocol StringUserDefaultable {
  23. associatedtype StringDefaultKey: RawRepresentable
  24. }
  25. extension StringUserDefaultable where StringDefaultKey.RawValue == String {
  26. public static func set(_ value: String, forKey key: StringDefaultKey) {
  27. let key = key.rawValue
  28. UserDefaults.standard.set(value, forKey: key)
  29. }
  30. public static func string(forKey key: StringDefaultKey) -> String {
  31. let key = key.rawValue
  32. return UserDefaults.standard.string(forKey: key) ?? ""
  33. }
  34. }
  35. public protocol DoubleUserDefaultable {
  36. associatedtype DoubleDefaultKey: RawRepresentable
  37. }
  38. extension DoubleUserDefaultable where DoubleDefaultKey.RawValue == String {
  39. public static func set(_ value: Double, forKey key: DoubleDefaultKey) {
  40. let key = key.rawValue
  41. UserDefaults.standard.set(value, forKey: key)
  42. }
  43. public static func double(forKey key: DoubleDefaultKey) -> Double {
  44. let key = key.rawValue
  45. return UserDefaults.standard.double(forKey: key)
  46. }
  47. }
  48. public protocol IntUserDefaultable {
  49. associatedtype IntDefaultKey: RawRepresentable
  50. }
  51. extension IntUserDefaultable where IntDefaultKey.RawValue == String {
  52. public static func set(_ value: Int, forKey key: IntDefaultKey) {
  53. let key = key.rawValue
  54. UserDefaults.standard.set(value, forKey: key)
  55. }
  56. public static func int(forKey key: IntDefaultKey) -> Int {
  57. let key = key.rawValue
  58. return UserDefaults.standard.integer(forKey: key)
  59. }
  60. }
  61. public protocol DataUserDefaultable {
  62. associatedtype DataDefaultKey: RawRepresentable
  63. }
  64. extension DataUserDefaultable where DataDefaultKey.RawValue == String {
  65. public static func set(_ value: Data, forKey key: DataDefaultKey) {
  66. let key = key.rawValue
  67. UserDefaults.standard.set(value, forKey: key)
  68. }
  69. public static func data(forKey key: DataDefaultKey) -> Data {
  70. let key = key.rawValue
  71. return UserDefaults.standard.data(forKey: key) ?? Data()
  72. }
  73. }
  74. public protocol ArrayUserDefaultCodable {
  75. init(value: [String: AnyObject])
  76. func toDictionary() -> [String: AnyObject]
  77. }
  78. public protocol ArrayUserDefaultable {
  79. associatedtype ArrayDefaultKey: RawRepresentable
  80. associatedtype Item: ArrayUserDefaultCodable
  81. }
  82. extension ArrayUserDefaultable where ArrayDefaultKey.RawValue == String {
  83. public static func set(_ value: [Item], forKey key: ArrayDefaultKey) {
  84. let key = key.rawValue
  85. UserDefaults.standard.set(value.flatMap { $0.toDictionary() }, forKey: key)
  86. }
  87. public static func array(forKey key: ArrayDefaultKey) -> [Item] {
  88. let key = key.rawValue
  89. guard let value = UserDefaults.standard.array(forKey: key) as? [[String: AnyObject]] else {
  90. return []
  91. }
  92. let items = value.compactMap { Item.init(value: $0) }
  93. return items
  94. }
  95. }
  96. public protocol DictionaryUserDefaultable {
  97. associatedtype DictionaryDefaultKey: RawRepresentable
  98. }
  99. extension DictionaryUserDefaultable where DictionaryDefaultKey.RawValue == String {
  100. public static func set(_ value: [String: Any], forKey key: DictionaryDefaultKey) {
  101. let key = key.rawValue
  102. UserDefaults.standard.set(value, forKey: key)
  103. }
  104. public static func dictionary(forKey key: DictionaryDefaultKey) -> [String: Any] {
  105. let key = key.rawValue
  106. return UserDefaults.standard.dictionary(forKey: key) ?? [String: Any]()
  107. }
  108. }