Advertisement
Revolucent

Swift Dictionary

Apr 3rd, 2017
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.47 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2.  
  3. import Cocoa
  4.  
  5. extension Array {
  6.     func dictionary<K: Hashable, V>() -> [K: V] where Element == Dictionary<K, V>.Element {
  7.         var dictionary = [K: V]()
  8.         for element in self {
  9.             dictionary[element.key] = element.value
  10.         }
  11.         return dictionary
  12.     }
  13. }
  14.  
  15. var dictionary = ["x": 7, "y": 3, "n": 14, "z": 9]
  16. dictionary = dictionary.filter{ $0.value % 7 == 0 }.dictionary()
  17. dictionary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement