swift - "Missing return" in function that finds the key with min/max value in dictionary -


i have defined following function find minimum key value in dictionary:

func keyminvalue(dictionary: [string: double])-> string{      (key,value) in dict{         if value == dict.values.minelement(){             return key         }     } } 

an error indicates code is:

missing return in function expected return 'string'

i've used same logic in python code before , works. new swift, there maybe missing here. function not break @ first instance of return?

you close. problem if there no elements in dictionary, returned? may consider making return value optional, , returning nil if no element found.

func keyminvalue(dict: [string: double]) -> string? {      (key, value) in dict {         if value == dict.values.minelement() {             return key         }     }      return nil } 

Comments