This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,708 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Foundation | ||
|
||
extension NSUserDefaults { | ||
|
||
//****************************************************************************// | ||
// MARK: Getters | ||
//****************************************************************************// | ||
|
||
public func getDecodable<T: Decodable>(key: String) -> T? { | ||
guard let dictionary = dictionaryForKey(key) else { return nil } | ||
return T.decode(dictionary) | ||
} | ||
|
||
public func getDecodable<T: Decodable>(key: String) -> [T]? { | ||
guard let array = arrayForKey(key) else { return nil } | ||
return sequence(array.map(T.decode)) | ||
} | ||
|
||
public func getDecodable<T: Decodable>(key: String) -> [String : T]? { | ||
guard let dictionary = dictionaryForKey(key) else { return nil } | ||
return sequence(dictionary.map { T.decode($0) }) | ||
} | ||
|
||
public func getDecodable<T: Decodable>(key: String, defaultValue: T) -> T { | ||
return getDecodable(key) ?? defaultValue | ||
} | ||
|
||
public func getDecodable<T: Decodable>(key: String, defaultValue: [T]) -> [T] { | ||
return getDecodable(key) ?? defaultValue | ||
} | ||
|
||
public func getDecodable<T: Decodable>(key: String, defaultValue: [String : T] | ||
) -> [String : T] { | ||
return getDecodable(key) ?? defaultValue | ||
} | ||
|
||
//****************************************************************************// | ||
// MARK: Setters | ||
//****************************************************************************// | ||
|
||
public func setEncodeable<V: Encodable>(value: V, forKey key: String) { | ||
setObject(value.encode(), forKey: key) | ||
} | ||
|
||
public func setValue<T: Encodable>(value: [T], forKey key: String) { | ||
setObject(value.map { $0.encode() }, forKey: key) | ||
} | ||
|
||
public func setValue<T: Encodable>(value: [String : T], forKey key: String) { | ||
setObject(value.map { $0.encode() }, forKey: key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.