Skip to content

Instantly share code, notes, and snippets.

@uruly
Created April 12, 2018 05:26
Show Gist options
  • Save uruly/8335c8ebe8fd4dd47bb9369f81faee67 to your computer and use it in GitHub Desktop.
Save uruly/8335c8ebe8fd4dd47bb9369f81faee67 to your computer and use it in GitHub Desktop.
extension Dictionary {
//only [String:Int]
func maxKey() -> [String]?{
guard let values = Array(self.values) as? [Int] else { return nil }
guard let _ = Array(self.keys) as? [String] else { return nil }
//valueの最大値を取り出す
guard let max = values.max() else { return nil }
var maxKeys:[String] = []
//辞書の中身をひとつずつ見ていく
for ( key,value ) in self{
if value as! Int == max{
print("dic key is \(key) , dic value is \(value)")
maxKeys.append(key as! String)
}
}
return maxKeys
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment