Created
June 21, 2015 05:47
-
-
Save muzahid59/cf5e5efc57f4859f8e71 to your computer and use it in GitHub Desktop.
Swift dictionary extension to get the values for multiple keys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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