Skip to content

Instantly share code, notes, and snippets.

@muzahid59
Created June 21, 2015 05:47
Show Gist options
  • Select an option

  • Save muzahid59/cf5e5efc57f4859f8e71 to your computer and use it in GitHub Desktop.

Select an option

Save muzahid59/cf5e5efc57f4859f8e71 to your computer and use it in GitHub Desktop.
Swift dictionary extension to get the values for multiple keys
extension Dictionary {
func valuesForKeys(keys: [Key])->[Value?]{
var result = [Value?]()
result.reserveCapacity(keys.count)
for key in keys{
result.append(self[key])
}
return result
}
}
// Implementation
var dic = [
"A" : "Apple",
"B" : "Banana",
"C" : "Clemon",
"D" : "Date"
]
let values = dic.valuesForKey(["A","D"])
// Output : [Apple,Date]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment