From 0424c5ea693cddc1c40c2560851a4221568dedd8 Mon Sep 17 00:00:00 2001 From: Ivan Smolin Date: Wed, 23 Nov 2016 09:10:27 +0300 Subject: [PATCH] fix issue with saving models with optional type --- .../UserDefaults+MappableDataTypes.swift | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/LeadKit/LeadKit/Extensions/UserDefaults/UserDefaults+MappableDataTypes.swift b/LeadKit/LeadKit/Extensions/UserDefaults/UserDefaults+MappableDataTypes.swift index 35d0aa64..8714cde7 100644 --- a/LeadKit/LeadKit/Extensions/UserDefaults/UserDefaults+MappableDataTypes.swift +++ b/LeadKit/LeadKit/Extensions/UserDefaults/UserDefaults+MappableDataTypes.swift @@ -100,20 +100,30 @@ public extension UserDefaults { return (try? object(forKey: key)) ?? defaultValue } - /// Sets the value of the specified default key in the standard application domain. + /// Sets or removes the value of the specified default key in the standard application domain. /// - /// - parameter model: The object with specified type to store in the defaults database. - /// - parameter key: The key with which to associate with the value. - public func set(_ model: T, forKey key: String) where T: ImmutableMappable { - set(model.toJSON(), forKey: key) + /// - Parameters: + /// - model: The object with specified type to store or nil to remove it from the defaults database. + /// - key: The key with which to associate with the value. + public func set(_ model: T?, forKey key: String) where T: ImmutableMappable { + if let model = model { + set(model.toJSON(), forKey: key) + } else { + set(nil, forKey: key) + } } - /// Sets the value of the specified default key in the standard application domain. + /// Sets or removes the value of the specified default key in the standard application domain. /// - /// - parameter models: The array of object with specified type to store in the defaults database. - /// - parameter key: The key with which to associate with the value. - public func set(_ models: S, forKey key: String) where T: ImmutableMappable, S: Sequence, S.Iterator.Element == T { - set(models.map { $0.toJSON() }, forKey: key) + /// - Parameters: + /// - models: The array of object with specified type to store or nil to remove it from the defaults database. + /// - key: The key with which to associate with the value. + public func set(_ models: S?, forKey key: String) where T: ImmutableMappable, S: Sequence, S.Iterator.Element == T { + if let models = models { + set(models.map { $0.toJSON() }, forKey: key) + } else { + set(nil, forKey: key) + } } } @@ -200,7 +210,7 @@ public extension Reactive where Base: UserDefaults { /// - parameter key: The key with which to associate with the value. /// /// - returns: Observable of Void type. - func set(_ model: T, forKey key: String) -> Observable where T: ImmutableMappable { + func set(_ model: T?, forKey key: String) -> Observable where T: ImmutableMappable { return Observable.create { observer in observer.onNext(self.base.set(model, forKey: key)) observer.onCompleted() @@ -217,7 +227,7 @@ public extension Reactive where Base: UserDefaults { /// - parameter key: The key with which to associate with the value. /// /// - returns: Observable of Void type. - func set(_ models: S, forKey key: String) -> Observable + func set(_ models: S?, forKey key: String) -> Observable where T: ImmutableMappable, S: Sequence, S.Iterator.Element == T { return Observable.create { observer in