@@ -30,7 +30,7 @@ import Foundation
3030/// ### UserDefault
3131/// A defined structure for storing information in `NSUserDefaults` and `NSUbiquitousKeyValueStore`.
3232/// The `timestamp` and optional `build` variables allow for comparisson during syncing.
33- public struct UserDefault {
33+ public struct KeyValueItem {
3434 public var value : NSObject
3535 public var timestamp : NSDate = NSDate ( )
3636 public var build : NSString ?
@@ -44,15 +44,16 @@ public struct UserDefault {
4444 }
4545}
4646
47- /// ### UserDefaults
48- /// A protocol declaring dictionary conformance. These methods are taken from `NSUbiquitousKeyValueStore`.
49- public protocol UserDefaults {
47+ /// ### KeyValueStorage
48+ /// A protocol declaring dictionary conformance.
49+ /// - note: These methods are taken from `NSUbiquitousKeyValueStore`.
50+ public protocol KeyValueStorage {
5051 func dictionaryForKey( aKey: String ) -> [ String : AnyObject ] ?
5152 func setDictionary( aDictionary: [ String : AnyObject ] ? , forKey aKey: String )
5253}
5354
54- extension UserDefaults {
55- func userDefaultForKey ( key: String ) -> UserDefault ? {
55+ extension KeyValueStorage {
56+ func itemForKey ( key: String ) -> KeyValueItem ? {
5657 guard let dictionary = self . dictionaryForKey ( key) else {
5758 return nil
5859 }
@@ -67,31 +68,30 @@ extension UserDefaults {
6768
6869 let build = dictionary [ KeyValueUbiquityContainer . Keys. build] as? NSString
6970
70- return UserDefault ( value: value, timestamp: timestamp, build: build)
71+ return KeyValueItem ( value: value, timestamp: timestamp, build: build)
7172 }
7273
7374 func valueForKey( key: String ) -> NSObject ? {
74- return userDefaultForKey ( key) ? . value
75+ return itemForKey ( key) ? . value
7576 }
7677
77- func setUserDefault ( userDefault : UserDefault , forKey key: String ) {
78+ func setItem ( item : KeyValueItem , forKey key: String ) {
7879 var dictionary = [ String : NSObject] ( )
79- dictionary [ KeyValueUbiquityContainer . Keys. value] = userDefault . value
80- dictionary [ KeyValueUbiquityContainer . Keys. timestamp] = userDefault . timestamp
81- if let build = userDefault . build {
80+ dictionary [ KeyValueUbiquityContainer . Keys. value] = item . value
81+ dictionary [ KeyValueUbiquityContainer . Keys. timestamp] = item . timestamp
82+ if let build = item . build {
8283 dictionary [ KeyValueUbiquityContainer . Keys. build] = build
8384 }
8485
8586 self . setDictionary ( dictionary, forKey: key)
8687 }
8788
8889 func setValue( value: NSObject , forKey key: String ) {
89- let userDefault = UserDefault ( value: value, timestamp: NSDate ( ) , build: nil )
90- self . setUserDefault ( userDefault, forKey: key)
90+ self . setItem ( KeyValueItem ( value: value, timestamp: NSDate ( ) , build: nil ) , forKey: key)
9191 }
9292}
9393
94- extension NSUserDefaults : UserDefaults {
94+ extension NSUserDefaults : KeyValueStorage {
9595 public func setDictionary( aDictionary: [ String : AnyObject ] ? , forKey aKey: String ) {
9696 if let dictionary = aDictionary {
9797 self . setObject ( dictionary, forKey: aKey)
@@ -101,12 +101,12 @@ extension NSUserDefaults: UserDefaults {
101101 }
102102}
103103
104- extension NSUbiquitousKeyValueStore : UserDefaults {
104+ extension NSUbiquitousKeyValueStore : KeyValueStorage {
105105}
106106
107107public protocol KeyValueUbiquityContainerDelegate {
108- func shouldReplaceUserDefaults ( existingDefualt oldValue : UserDefault , withDefault newValue : UserDefault , forKey key: String ) -> Bool
109- func didSetUserDefault ( userDefault : UserDefault , forKey key: String )
108+ func shouldReplaceItem ( existingItem existingItem : KeyValueItem , withItem newItem : KeyValueItem , forKey key: String ) -> Bool
109+ func didSetItem ( item : KeyValueItem , forKey key: String )
110110}
111111
112112/// ### KeyValueUbiquityContainer
@@ -165,29 +165,29 @@ public class KeyValueUbiquityContainer: UbiquityContainer {
165165 }
166166
167167 for key in changedKeys {
168- guard let ubiquityUserDefault = keyValueStore. userDefaultForKey ( key) else {
168+ guard let ubiquityItem = keyValueStore. itemForKey ( key) else {
169169 Logger . debug ( " Removing NSUserDefaults object for key ' \( key) ' " )
170170 NSUserDefaults . standardUserDefaults ( ) . removeObjectForKey ( key)
171171 continue
172172 }
173173
174- guard let standardUserDefault = NSUserDefaults . standardUserDefaults ( ) . userDefaultForKey ( key) else {
175- NSUserDefaults . standardUserDefaults ( ) . setUserDefault ( ubiquityUserDefault , forKey: key)
174+ guard let standardItem = NSUserDefaults . standardUserDefaults ( ) . itemForKey ( key) else {
175+ NSUserDefaults . standardUserDefaults ( ) . setItem ( ubiquityItem , forKey: key)
176176 if let delegate = keyValueDelegate {
177- delegate. didSetUserDefault ( ubiquityUserDefault , forKey: key)
177+ delegate. didSetItem ( ubiquityItem , forKey: key)
178178 }
179179 continue
180180 }
181181
182182 var replace = true
183183 if let delegate = self . keyValueDelegate {
184- replace = delegate. shouldReplaceUserDefaults ( existingDefualt : standardUserDefault , withDefault : ubiquityUserDefault , forKey: key)
184+ replace = delegate. shouldReplaceItem ( existingItem : standardItem , withItem : ubiquityItem , forKey: key)
185185 }
186186
187187 if replace {
188- NSUserDefaults . standardUserDefaults ( ) . setUserDefault ( ubiquityUserDefault , forKey: key)
188+ NSUserDefaults . standardUserDefaults ( ) . setItem ( ubiquityItem , forKey: key)
189189 if let delegate = keyValueDelegate {
190- delegate. didSetUserDefault ( ubiquityUserDefault , forKey: key)
190+ delegate. didSetItem ( ubiquityItem , forKey: key)
191191 }
192192 }
193193 }
@@ -201,19 +201,24 @@ public class KeyValueUbiquityContainer: UbiquityContainer {
201201public extension NSUserDefaults {
202202 public static var ubiquityUserDefaults : KeyValueUbiquityContainer = KeyValueUbiquityContainer ( )
203203
204- public static func setUserDefault( userDefault: UserDefault , forKey key: String ) {
204+ /// Attempts to set an item on `NSUbiquitousKeyValueStore`. Will fallback to `NSUserDefaults`
205+ public static func setItem( item: KeyValueItem , forKey key: String ) {
205206 if let keyValueStore = NSUserDefaults . ubiquityUserDefaults. keyValueStore {
206- keyValueStore. setUserDefault ( userDefault, forKey: key)
207+ keyValueStore. setItem ( item, forKey: key)
208+ if let delegate = ubiquityUserDefaults. keyValueDelegate {
209+ delegate. didSetItem ( item, forKey: key)
210+ }
207211 } else {
208- NSUserDefaults . standardUserDefaults ( ) . setUserDefault ( userDefault , forKey: key)
212+ NSUserDefaults . standardUserDefaults ( ) . setItem ( item , forKey: key)
209213 }
210214 }
211215
212- public static func userDefaultForKey( key: String ) -> UserDefault ? {
213- if let userDefault = NSUserDefaults . ubiquityUserDefaults. keyValueStore? . userDefaultForKey ( key) {
214- return userDefault
216+ /// Attemps to retrieve an item from `NSUbiquitousKeyValueStore`. Will fallback to `NSUserDefaults`
217+ public static func itemForKey( key: String ) -> KeyValueItem ? {
218+ if let item = NSUserDefaults . ubiquityUserDefaults. keyValueStore? . itemForKey ( key) {
219+ return item
215220 }
216221
217- return NSUserDefaults . standardUserDefaults ( ) . userDefaultForKey ( key)
222+ return NSUserDefaults . standardUserDefaults ( ) . itemForKey ( key)
218223 }
219224}
0 commit comments