Guest User

Untitled

a guest
Jan 24th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment