From 157f95102197e87c6e2ac6619517362580058a4a Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Thu, 21 Mar 2019 14:18:57 -0400 Subject: [PATCH 1/7] Applied code guidelines for consistent code. --- .../Categories/Dictionary+MFConvenience.swift | 92 ++++++++----------- 1 file changed, 36 insertions(+), 56 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index fa54073..e34c3a9 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -8,52 +8,48 @@ import Foundation + public extension Dictionary { /// Returns a Dictionary using the specified chain. An empty dictionary is returned if a dictionary for the chain does not exist - public func dictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String : Any] { + public func dictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any] { - guard let dictionary = optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) else { - return [:] - } + guard let dictionary = optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) else { return [:] } return dictionary } /// Returns an optional Dictionary using the specified chain. Returns nil if a dictionary for the chain does not exist - public func optionalDictionaryWithChainOfKeysOrIndexes (_ keysOrIndexes: [Any]) -> [String : Any]? { + public func optionalDictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any]? { return objectChainOfKeysOrIndexes(keysOrIndexes) as? [String: Any] } - public func optionalDictionaryForKey(_ key: String) -> [String : Any]? { + public func optionalDictionaryForKey(_ key: String) -> [String: Any]? { return objectChainOfKeysOrIndexes([key]) as? [String: Any] } /// Returns a String using the specified chain. An empty string is returned if a string for the chain does not exist - public func stringWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String { + public func stringWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String { - guard let string = objectChainOfKeysOrIndexes(keysOrIndexes) as? String else { - return "" - } + guard let string = objectChainOfKeysOrIndexes(keysOrIndexes) as? String else { return "" } return string } - public func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String? { + public func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String? { + return objectChainOfKeysOrIndexes(keysOrIndexes) as? String } /// Returns an Array using the specified chain. Returns an empty array if an array for the chain does not exist public func arrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any] { - guard let array = objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] else { - return [] - } + guard let array = objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] else { return [] } return array } /// Returns a Dictionary after looking up the specified key. An empty dictionary is returned if a dictionary for the key does not exist - public func dictionaryForKey(_ key : String) -> [String : Any] { + public func dictionaryForKey(_ key: String) -> [String: Any] { return dictionaryWithChainOfKeysOrIndexes([key]) } @@ -66,104 +62,87 @@ public extension Dictionary { /// Returns a String after looking up the specified key. Nil will be returned if a string for the key does not exist public func optionalStringForKey(_ key: String) -> String? { + return objectChainOfKeysOrIndexes([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 - public func arrayForKey(_ key : String) -> [Any] { + public func arrayForKey(_ key: String) -> [Any] { return arrayForChainOfKeysOrIndexes([key]) } /// Return a Bool after looking up the specified key. This will return false if the key does not exist - public func boolForKey(_ key : String) -> Bool { + public func boolForKey(_ key: String) -> Bool { - guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { - return false - } + guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { return false } return bool } /// Return a Bool after looking up the specified key. This will return false if the key does not exist - public func optionalBoolForKey(_ key : String) -> Bool? { + public func optionalBoolForKey(_ key: String) -> Bool? { - guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { - return nil - } + guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { return nil } return bool } public func lenientBoolForKey(_ key: String) -> Bool { - guard let key = key as? Key, let object = self[key] else { - return false - } + + guard let key = key as? Key, let object = self[key] else { return false } + if let object = object as? NSNumber { return object.boolValue } else if let object = object as? NSString { return object.boolValue } + return false } - public func boolForChainOfKeysOrIndexes(_ keysOrIndexes:[Any])-> Bool { - guard let bool = objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool else { - return false - } + public func boolForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Bool { + + guard let bool = objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool else { return false } return bool } /// Return a float from a string created by looking up the specified key. This will return 0.0 if the key does not exist public func floatForKey(_ key: String) -> Float { - guard let floatValue = objectChainOfKeysOrIndexes([key]) as? Float else { - return 0.0 - } - + guard let floatValue = objectChainOfKeysOrIndexes([key]) as? Float else { return 0.0 } return floatValue } - public func floatFromStringForKey(_ key:String) -> Float { + public func floatFromStringForKey(_ key: String) -> Float { let stringValue = stringForkey(key) - guard let floatValue = Float(stringValue) else { - return 0.0 - } - + guard let floatValue = Float(stringValue) else { return 0.0 } return floatValue } - - public func int32ForKey(_ key:String) -> Int32 { - - guard let intValue = objectChainOfKeysOrIndexes([key]) as? Int32 else { - return 0 - } + public func int32ForKey(_ key: String) -> Int32 { + guard let intValue = objectChainOfKeysOrIndexes([key]) as? Int32 else { return 0 } return intValue } - private func objectChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> Any? { + private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? { - var previousObject : Any? = self + var previousObject: Any? = self for keyOrIndex in keysOrIndexes { - if let nextDictionary = previousObject as? [String:Any], - let keyOrIndex = keyOrIndex as? String { + if let nextDictionary = previousObject as? [String: Any], let keyOrIndex = keyOrIndex as? String { previousObject = nextDictionary[keyOrIndex] continue - } - else if let nextArray = previousObject as? [Any], - let keyOrIndex = keyOrIndex as? Int { + } else if let nextArray = previousObject as? [Any], let keyOrIndex = keyOrIndex as? Int { previousObject = nextArray[keyOrIndex] continue - } - else { + + } else { previousObject = nil break } @@ -171,4 +150,5 @@ public extension Dictionary { return previousObject } + } From 6263720e3d0342e1b86cf75ca510544ce90a0792 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Thu, 2 May 2019 14:06:51 -0400 Subject: [PATCH 2/7] updated take on dictionary extension. Removed unnecessary calls to optionalChain func. Simplified method internals. --- .../Categories/Dictionary+MFConvenience.swift | 143 ++++++++---------- 1 file changed, 61 insertions(+), 82 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index 9f3af95..5f1c096 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -11,179 +11,158 @@ import Foundation public extension 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] { + func dictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any] { - guard let dictionary = optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) else { - return [:] - } - return dictionary + return optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) ?? [:] } /// Returns an optional Dictionary using the specified chain. Returns nil if a dictionary for the chain does not exist - func optionalDictionaryWithChainOfKeysOrIndexes (_ keysOrIndexes: [Any]) -> [String : Any]? { + func optionalDictionaryWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [String: Any]? { return objectChainOfKeysOrIndexes(keysOrIndexes) as? [String: Any] } - func optionalDictionaryForKey(_ key: String) -> [String : Any]? { + func optionalDictionaryForKey(_ key: String) -> [String: Any]? { - return objectChainOfKeysOrIndexes([key]) as? [String: Any] + guard let key = key as? Key else { return nil } + + return self[key] as? [String: Any] } /// Returns a String using the specified chain. An empty string is returned if a string for the chain does not exist - func stringWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String { + func stringWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String { - guard let string = objectChainOfKeysOrIndexes(keysOrIndexes) as? String else { - return "" - } - return string + return objectChainOfKeysOrIndexes(keysOrIndexes) as? String ?? "" } - func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String? { + func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String? { + return objectChainOfKeysOrIndexes(keysOrIndexes) as? String } /// 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] { - guard let array = objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] else { - return [] - } - return array + return objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] ?? [] } func optionalArrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any]? { + 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] { + func dictionaryForKey(_ key: String) -> [String: Any] { - return dictionaryWithChainOfKeysOrIndexes([key]) + guard let key = key as? Key else { return [:] } + + return self[key] as? [String: Any] ?? [:] } /// 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 stringWithChainOfKeysOrIndexes([key]) + guard let key = key as? Key else { return "" } + + return self[key] as? String ?? "" } /// 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? { + return objectChainOfKeysOrIndexes([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] { + func arrayForKey(_ key: String) -> [Any] { - return arrayForChainOfKeysOrIndexes([key]) + guard let key = key as? Key else { return [] } + + return self[key] as? [Any] ?? [] } - func optionalArrayForKey(_ key : String) -> [Any]? { - guard let key = key as? Key else { - return nil - } + func optionalArrayForKey(_ key: String) -> [Any]? { + + guard let key = key as? Key else { return nil } + return self[key] as? [Any] } /// Return a Bool after looking up the specified key. This will return false if the key does not exist - func boolForKey(_ key : String) -> Bool { + func boolForKey(_ key: String) -> Bool { - guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { - return false - } - return bool + guard let key = key as? Key else { return false } + + return self[key] as? Bool ?? false } /// Return a Bool after looking up the specified key. This will return false if the key does not exist - func optionalBoolForKey(_ key : String) -> Bool? { + func optionalBoolForKey(_ key: String) -> Bool? { - guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else { - return nil - } - return bool + guard let key = key as? Key else { return nil } + + return self[key] as? Bool ?? nil } func lenientBoolForKey(_ key: String) -> Bool { - guard let key = key as? Key, let object = self[key] else { - return false - } + + guard let key = key as? Key, let object = self[key] else { return false } + if let object = object as? NSNumber { return object.boolValue + } else if let object = object as? NSString { return object.boolValue } + return false } - func boolForChainOfKeysOrIndexes(_ keysOrIndexes:[Any])-> Bool { - guard let bool = objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool else { - return false - } - return bool + func boolForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Bool { + + return objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool ?? false } /// 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 floatValue = objectChainOfKeysOrIndexes([key]) as? Float else { - return 0.0 - } - - return floatValue + return objectChainOfKeysOrIndexes([key]) as? Float ?? 0.0 } func optionalCGFloatForKey(_ key: String) -> CGFloat? { - guard let key = key as? Key else { - return nil - } + + guard let key = key as? Key else { return nil } + return self[key] as? CGFloat } - func floatFromStringForKey(_ key:String) -> Float { + func floatFromStringForKey(_ key: String) -> Float { - let stringValue = stringForkey(key) - - guard let floatValue = Float(stringValue) else { - return 0.0 - } - - return floatValue + return Float(stringForkey(key)) ?? 0.0 } - - func int32ForKey(_ key:String) -> Int32 { + func int32ForKey(_ key: String) -> Int32 { - guard let intValue = objectChainOfKeysOrIndexes([key]) as? Int32 else { - return 0 - } + guard let key = key as? Key else { return 0 } - return intValue + return self[key] as? Int32 ?? 0 } - private func objectChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> Any? { + private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? { - var previousObject : Any? = self + var previousObject: Any? = self for keyOrIndex in keysOrIndexes { - - if let nextDictionary = previousObject as? [String:Any], + if let nextDictionary = previousObject as? [String: Any], let keyOrIndex = keyOrIndex as? String { - previousObject = nextDictionary[keyOrIndex] - continue - } - else if let nextArray = previousObject as? [Any], + } else if let nextArray = previousObject as? [Any], let keyOrIndex = keyOrIndex as? Int { - previousObject = nextArray[keyOrIndex] - continue - } - else { - previousObject = nil - break + + } else { + return nil } } From a6ee9ca7757906d3688bdb224eb05f48a648dd16 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Tue, 7 May 2019 10:47:04 -0400 Subject: [PATCH 3/7] changes made --- .../Categories/Dictionary+MFConvenience.swift | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index 5f1c096..0ffad8d 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -54,17 +54,13 @@ public extension 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] { - guard let key = key as? Key else { return [:] } - - return self[key] as? [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 { - guard let key = key as? Key else { return "" } - - return self[key] as? 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 @@ -76,9 +72,7 @@ public extension Dictionary { /// 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] { - guard let key = key as? Key else { return [] } - - return self[key] as? [Any] ?? [] + return optionalArrayForKey(key) ?? [] } func optionalArrayForKey(_ key: String) -> [Any]? { @@ -91,9 +85,7 @@ public extension Dictionary { /// Return a Bool after looking up the specified key. This will return false if the key does not exist func boolForKey(_ key: String) -> Bool { - guard let key = key as? Key else { return false } - - return self[key] as? Bool ?? false + return optionalBoolForKey(key) ?? false } /// Return a Bool after looking up the specified key. This will return false if the key does not exist @@ -101,7 +93,7 @@ public extension Dictionary { guard let key = key as? Key else { return nil } - return self[key] as? Bool ?? nil + return self[key] as? Bool } func lenientBoolForKey(_ key: String) -> Bool { From a63997fa09fa3281112778dbcd1a6be9a3de5215 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Tue, 7 May 2019 14:08:24 -0400 Subject: [PATCH 4/7] correction. --- MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index 0ffad8d..baf1d18 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -66,7 +66,9 @@ public extension Dictionary { /// 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? { - return objectChainOfKeysOrIndexes([key]) as? 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 @@ -118,7 +120,9 @@ public extension Dictionary { /// 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 { - return objectChainOfKeysOrIndexes([key]) as? Float ?? 0.0 + guard let key = key as? Key else { return 0.0 } + + return self[key] as? Float ?? 0.0 } func optionalCGFloatForKey(_ key: String) -> CGFloat? { From 633cda9f9fcf82938d1e16b79f80f368cb888924 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Tue, 7 May 2019 15:55:48 -0400 Subject: [PATCH 5/7] Corrected some issues. --- .../Categories/Dictionary+MFConvenience.swift | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index baf1d18..2dad46b 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -120,16 +120,13 @@ public extension Dictionary { /// 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 } - - return self[key] as? Float ?? 0.0 + return Float(stringForkey(key)) ?? 0.0 } func optionalCGFloatForKey(_ key: String) -> CGFloat? { - guard let key = key as? Key else { return nil } - - return self[key] as? CGFloat + guard let float = Float(stringForkey(key)) else { return nil } + return CGFloat(float) } func floatFromStringForKey(_ key: String) -> Float { @@ -139,9 +136,7 @@ public extension Dictionary { func int32ForKey(_ key: String) -> Int32 { - guard let key = key as? Key else { return 0 } - - return self[key] as? Int32 ?? 0 + return Int32(stringForkey(key)) ?? 0 } private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? { From e199cd9c02b6504e56740fa02dfa578df40d7869 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Tue, 7 May 2019 16:05:46 -0400 Subject: [PATCH 6/7] go back. --- .../Categories/Dictionary+MFConvenience.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index 2dad46b..baf1d18 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -120,13 +120,16 @@ public extension Dictionary { /// 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 { - return Float(stringForkey(key)) ?? 0.0 + guard let key = key as? Key else { return 0.0 } + + return self[key] as? Float ?? 0.0 } func optionalCGFloatForKey(_ key: String) -> CGFloat? { - guard let float = Float(stringForkey(key)) else { return nil } - return CGFloat(float) + guard let key = key as? Key else { return nil } + + return self[key] as? CGFloat } func floatFromStringForKey(_ key: String) -> Float { @@ -136,7 +139,9 @@ public extension Dictionary { func int32ForKey(_ key: String) -> Int32 { - return Int32(stringForkey(key)) ?? 0 + guard let key = key as? Key else { return 0 } + + return self[key] as? Int32 ?? 0 } private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? { From a1a623aba0120378db478794a8c4882aa61a590e Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Thu, 9 May 2019 14:35:32 -0400 Subject: [PATCH 7/7] Reorganization. --- .../Categories/Dictionary+MFConvenience.swift | 122 +++++++++++------- 1 file changed, 75 insertions(+), 47 deletions(-) diff --git a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift index baf1d18..2b467aa 100644 --- a/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift +++ b/MVMCore/MVMCore/Categories/Dictionary+MFConvenience.swift @@ -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