Merge branch 'feature/kevin' into 'develop'
Updated Swift Dictionary Extension See merge request BPHV_MIPS/mvm_core!13
This commit is contained in:
commit
c5f1785923
@ -8,182 +8,185 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
public extension Dictionary {
|
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
|
/// 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 optionalDictionaryWithChainOfKeysOrIndexes(keysOrIndexes) ?? [:]
|
||||||
return [:]
|
|
||||||
}
|
|
||||||
return dictionary
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an optional Dictionary using the specified chain. Returns nil if a dictionary for the chain does not exist
|
/// 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]
|
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, let dictionary = self[key] as? [String: Any] else { return nil }
|
||||||
|
|
||||||
|
return dictionary
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a String using the specified chain. An empty string is returned if a string for the chain does not exist
|
/// Returns a Dictionary after looking up the specified key. An empty dictionary is returned if a dictionary for the key does not exist
|
||||||
func stringWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String {
|
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 }
|
||||||
|
|
||||||
guard let string = objectChainOfKeysOrIndexes(keysOrIndexes) as? String else {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return string
|
return string
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> String? {
|
/// 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 {
|
||||||
|
|
||||||
|
return objectChainOfKeysOrIndexes(keysOrIndexes) as? String ?? ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringOptionalWithChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> String? {
|
||||||
|
|
||||||
return objectChainOfKeysOrIndexes(keysOrIndexes) as? String
|
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
|
/// 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] {
|
func arrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any] {
|
||||||
|
|
||||||
guard let array = objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] else {
|
return objectChainOfKeysOrIndexes(keysOrIndexes) as? [Any] ?? []
|
||||||
return []
|
|
||||||
}
|
|
||||||
return array
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func optionalArrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any]? {
|
func optionalArrayForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> [Any]? {
|
||||||
|
|
||||||
return objectChainOfKeysOrIndexes(keysOrIndexes) as? [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] {
|
// MARK:- Bool
|
||||||
|
//-------------------------------------------------
|
||||||
|
|
||||||
return dictionaryWithChainOfKeysOrIndexes([key])
|
/// Return a Bool after looking up the specified key. This will return false if the key does not exist
|
||||||
}
|
func boolForKey(_ key: String) -> Bool {
|
||||||
|
|
||||||
/// Returns a String after looking up the specified key. An empty string will be returned if a string for the key does not exist
|
return optionalBoolForKey(key) ?? false
|
||||||
func stringForkey(_ key: String) -> String {
|
|
||||||
|
|
||||||
return stringWithChainOfKeysOrIndexes([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? {
|
|
||||||
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] {
|
|
||||||
|
|
||||||
return arrayForChainOfKeysOrIndexes([key])
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
/// 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 optionalBoolForKey(_ key: String) -> Bool? {
|
||||||
|
|
||||||
guard let bool = objectChainOfKeysOrIndexes([key]) as? Bool else {
|
guard let key = key as? Key, let bool = self[key] as? Bool else { return nil }
|
||||||
return false
|
|
||||||
}
|
|
||||||
return bool
|
|
||||||
}
|
|
||||||
|
|
||||||
/// 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 bool = objectChainOfKeysOrIndexes([key]) as? Bool else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return bool
|
return bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func lenientBoolForKey(_ key: String) -> Bool {
|
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 {
|
if let object = object as? NSNumber {
|
||||||
return object.boolValue
|
return object.boolValue
|
||||||
|
|
||||||
} else if let object = object as? NSString {
|
} else if let object = object as? NSString {
|
||||||
return object.boolValue
|
return object.boolValue
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func boolForChainOfKeysOrIndexes(_ keysOrIndexes:[Any])-> Bool {
|
func boolForChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Bool {
|
||||||
guard let bool = objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool else {
|
|
||||||
return false
|
return objectChainOfKeysOrIndexes(keysOrIndexes) as? Bool ?? false
|
||||||
}
|
|
||||||
return bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------
|
||||||
|
// 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
|
/// 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 {
|
func floatForKey(_ key: String) -> Float {
|
||||||
|
|
||||||
guard let floatValue = objectChainOfKeysOrIndexes([key]) as? Float else {
|
guard let key = key as? Key, let float = self[key] as? Float else { return 0.0 }
|
||||||
return 0.0
|
|
||||||
}
|
|
||||||
|
|
||||||
return floatValue
|
return float
|
||||||
|
}
|
||||||
|
|
||||||
|
func floatFromStringForKey(_ key: String) -> Float {
|
||||||
|
|
||||||
|
return Float(stringForkey(key)) ?? 0.0
|
||||||
}
|
}
|
||||||
|
|
||||||
func optionalCGFloatForKey(_ key: String) -> CGFloat? {
|
func optionalCGFloatForKey(_ key: String) -> CGFloat? {
|
||||||
guard let key = key as? Key else {
|
|
||||||
return nil
|
guard let key = key as? Key, let float = self[key] as? CGFloat else { return nil }
|
||||||
}
|
|
||||||
return self[key] as? CGFloat
|
return float
|
||||||
}
|
}
|
||||||
|
|
||||||
func floatFromStringForKey(_ key:String) -> Float {
|
//-------------------------------------------------
|
||||||
|
// MARK:- Int
|
||||||
|
//-------------------------------------------------
|
||||||
|
|
||||||
let stringValue = stringForkey(key)
|
func int32ForKey(_ key: String) -> Int32 {
|
||||||
|
|
||||||
guard let floatValue = Float(stringValue) else {
|
guard let key = key as? Key, let integer = self[key] as? Int32 else { return 0 }
|
||||||
return 0.0
|
|
||||||
}
|
|
||||||
|
|
||||||
return floatValue
|
return integer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------
|
||||||
|
// MARK:- Drill-Down to Key or Index
|
||||||
|
//-------------------------------------------------
|
||||||
|
|
||||||
func int32ForKey(_ key:String) -> Int32 {
|
private func objectChainOfKeysOrIndexes(_ keysOrIndexes: [Any]) -> Any? {
|
||||||
|
|
||||||
guard let intValue = objectChainOfKeysOrIndexes([key]) as? Int32 else {
|
var previousObject: Any? = self
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return intValue
|
|
||||||
}
|
|
||||||
|
|
||||||
private func objectChainOfKeysOrIndexes(_ keysOrIndexes:[Any]) -> Any? {
|
|
||||||
|
|
||||||
var previousObject : Any? = self
|
|
||||||
|
|
||||||
for keyOrIndex in keysOrIndexes {
|
for keyOrIndex in keysOrIndexes {
|
||||||
|
if let nextDictionary = previousObject as? [String: Any],
|
||||||
if let nextDictionary = previousObject as? [String:Any],
|
|
||||||
let keyOrIndex = keyOrIndex as? String {
|
let keyOrIndex = keyOrIndex as? String {
|
||||||
|
|
||||||
previousObject = nextDictionary[keyOrIndex]
|
previousObject = nextDictionary[keyOrIndex]
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
else if let nextArray = previousObject as? [Any],
|
} else if let nextArray = previousObject as? [Any],
|
||||||
let keyOrIndex = keyOrIndex as? Int {
|
let keyOrIndex = keyOrIndex as? Int {
|
||||||
|
|
||||||
previousObject = nextArray[keyOrIndex]
|
previousObject = nextArray[keyOrIndex]
|
||||||
continue
|
|
||||||
}
|
} else {
|
||||||
else {
|
return nil
|
||||||
previousObject = nil
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user