I've been tasked with writing a basic "Thesaurus" app with just a few entries, but I don't know how to write code that can return a result for for when the key passed through my function has a nil value.
Eventually, the idea is to connect a text field input to a text view output so that the user can enter any word to try to get a response. But I can't even dial in my code without connecting it to a user interface.
let synonyms = ["swift" : ["abrupt", "expeditious", "hasty", "nimble", "quick", "rapid", "speedy", "sudden", "unexpected"]]
func buttonPressed(key:String)-> Array<Any> {
return synonyms[key]!
}
buttonPressed(key: "swift")
I can't make this run without force unwrapping, but I will eventually want this function to be able to return a string for a nil input that says "I'm sorry- this word is not in the Thesaurus."
I couldn't figure out what my "else" should be if I used "if let" to unwrap; and I couldn't figure out how to return an array in the case that my key has a value in my dictionary, but return a string in the nil case.
Any advice? Thanks!