Reorganization.

This commit is contained in:
Christiano, Kevin 2019-05-09 14:35:32 -04:00
parent e199cd9c02
commit a1a623aba0

View File

@ -8,7 +8,11 @@
import Foundation
public extension Dictionary {
//-------------------------------------------------
// MARK:- Dictionary
//-------------------------------------------------
/// Returns a Dictionary using the specified chain. An empty dictionary is returned if a dictionary for the chain does not exist
func dictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any] {
@ -24,9 +28,33 @@ public extension Dictionary {
func optionalDictionaryForKey(_ key: String) -> [String: Any]? {
guard let key = key as? Key else { return nil }
guard let key = key as? Key, let dictionary = self[key] as? [String: Any] else { return nil }
return self[key] as? [String: Any]
return dictionary
}
/// Returns a Dictionary after looking up the specified key. An empty dictionary is returned if a dictionary for the key does not exist
func dictionaryForKey(_ key: String) -> [String: Any] {
return optionalDictionaryForKey(key) ?? [:]
}
//-------------------------------------------------
// MARK:- String
//-------------------------------------------------
/// Returns a String after looking up the specified key. An empty string will be returned if a string for the key does not exist
func stringForkey(_ key: String) -> String {
return optionalStringForKey(key) ?? ""
}
/// Returns a String after looking up the specified key. Nil will be returned if a string for the key does not exist
func optionalStringForKey(_ key: String) -> String? {
guard let key = key as? Key, let string = self[key] as? String else { return nil }
return string
}
/// Returns a String using the specified chain. An empty string is returned if a string for the chain does not exist
@ -40,6 +68,23 @@ public extension Dictionary {
return objectChainOfKeysOrIndexes(keysOrIndexes) as? String
}
//-------------------------------------------------
// MARK:- Array
//-------------------------------------------------
/// Returns an Array after looking up the specified key. An empty array will be returned if an array for the key does not exist
func arrayForKey(_ key: String) -> [Any] {
return optionalArrayForKey(key) ?? []
}
func optionalArrayForKey(_ key: String) -> [Any]? {
guard let key = key as? Key, let array = self[key] as? [Any] else { return nil }
return array
}
/// Returns an Array using the specified chain. Returns an empty array if an array for the chain does not exist
func arrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any] {
@ -51,38 +96,9 @@ public extension Dictionary {
return objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any]
}
/// Returns a Dictionary after looking up the specified key. An empty dictionary is returned if a dictionary for the key does not exist
func dictionaryForKey(_ key: String) -> [String: Any] {
return optionalDictionaryForKey(key) ?? [:]
}
/// Returns a String after looking up the specified key. An empty string will be returned if a string for the key does not exist
func stringForkey(_ key: String) -> String {
return optionalStringForKey(key) ?? ""
}
/// Returns a String after looking up the specified key. Nil will be returned if a string for the key does not exist
func optionalStringForKey(_ key: String) -> String? {
guard let key = key as? Key else { return nil }
return self[key] as? String
}
/// Returns an Array after looking up the specified key. An empty array will be returned if an array for the key does not exist
func arrayForKey(_ key: String) -> [Any] {
return optionalArrayForKey(key) ?? []
}
func optionalArrayForKey(_ key: String) -> [Any]? {
guard let key = key as? Key else { return nil }
return self[key] as? [Any]
}
//-------------------------------------------------
// MARK:- Bool
//-------------------------------------------------
/// Return a Bool after looking up the specified key. This will return false if the key does not exist
func boolForKey(_ key: String) -> Bool {
@ -93,9 +109,9 @@ public extension Dictionary {
/// Return a Bool after looking up the specified key. This will return false if the key does not exist
func optionalBoolForKey(_ key: String) -> Bool? {
guard let key = key as? Key else { return nil }
guard let key = key as? Key, let bool = self[key] as? Bool else { return nil }
return self[key] as? Bool
return bool
}
func lenientBoolForKey(_ key: String) -> Bool {
@ -117,19 +133,16 @@ public extension Dictionary {
return objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool ?? false
}
//-------------------------------------------------
// MARK:- Float
//-------------------------------------------------
/// Return a float from a string created by looking up the specified key. This will return 0.0 if the key does not exist
func floatForKey(_ key: String) -> Float {
guard let key = key as? Key else { return 0.0 }
guard let key = key as? Key, let float = self[key] as? Float else { return 0.0 }
return self[key] as? Float ?? 0.0
}
func optionalCGFloatForKey(_ key: String) -> CGFloat? {
guard let key = key as? Key else { return nil }
return self[key] as? CGFloat
return float
}
func floatFromStringForKey(_ key: String) -> Float {
@ -137,13 +150,28 @@ public extension Dictionary {
return Float(stringForkey(key)) ?? 0.0
}
func optionalCGFloatForKey(_ key: String) -> CGFloat? {
guard let key = key as? Key, let float = self[key] as? CGFloat else { return nil }
return float
}
//-------------------------------------------------
// MARK:- Int
//-------------------------------------------------
func int32ForKey(_ key: String) -> Int32 {
guard let key = key as? Key else { return 0 }
guard let key = key as? Key, let integer = self[key] as? Int32 else { return 0 }
return self[key] as? Int32 ?? 0
return integer
}
//-------------------------------------------------
// MARK:- Drill-Down to Key or Index
//-------------------------------------------------
private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? {
var previousObject: Any? = self