daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Merges right hand side dictionary into left hand side dictionary. Works on nested dictionaries as well.
  2. ///
  3. /// - Parameters:
  4. ///   - lhs: Dictionary you want to merge someting.
  5. ///   - rhs: Merging dictionary.
  6. /// - Returns: Returns merged dictionary.
  7. internal func +<Key, Value> (lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value] {
  8.   var result = lhs
  9.   rhs.forEach {
  10.     if let dict = $1 as? [Key: Value] {
  11.       if let exist = result[$0] as? [Key: Value] {
  12.         result[$0] = exist + dict as? Value
  13.       } else {
  14.         result[$0] = dict as? Value
  15.       }
  16.     } else {
  17.       result[$0] = $1
  18.     }
  19.   }
  20.   return result
  21. }
  22.  
  23. /// Appends the right hand side dictionary. Works on nested dictionaries as well
  24. ///
  25. /// - Parameters:
  26. ///   - lhs: Dictionary you want to merge someting.
  27. ///   - rhs: Merging dictionary.
  28. internal func +=<Key, Value> (lhs: inout [Key: Value], rhs: [Key: Value]) {
  29.   // swiftlint:disable:next shorthand_operator
  30.   lhs = lhs + rhs
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top