Swift Question

How print some keys from a dictionary Swift

I'm experimenting with a little bit of code, this is what I want: I want to print the keys from a dictionary but only if this keys have a value more than 10. This is my code:

var alumnos = ["jose": 20, "leo": 56, "hadad": 8]


In this case only "jose" and "leo" have to be printed, because their values are more than 10.

Answer

Have you tried something like this:

for (key, value) in alumnos {
   if value > 10 {
      print(key)
   }
}

You can also turn it into a function like so:

func printKeyForValueGreaterThan10<T: Hashable>(dict: [T: Int]) {
  for (key, value) in dict {
    if value > 10 {
      print(key)
    }
  }
}

And call it by passing your dictionary.

printKeyForValueGreaterThan10(dict: alumnos)