0

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!

3 Answers3

1

A dictionary always returns an optional because the key may not exist. Think of your dictionary as [String: [String]?]

You can unwrap it like so with a nil coalescing operator. ?? operator in Swift

func buttonPressed(key:String)-> Array<Any> {
       return synonyms[key] ?? ["I'm sorry- this word is not in the Thesaurus."] 
}
Mocha
  • 2,035
  • 12
  • 29
1

First to logic. Since your dictionary is [String:[String]] you need to return array of String as value for string key

  `String` ˇ
synonyms[key] // returns synonyms
^ dictonary       ^ `[String]`

So you have this idea: You have text field with input, button and text view. First of all you need to register when button is pressed. So create action for event .touchUpInside

@IBAction func buttonPressed(_ sender: UIButton) {
}

So you can get text of your text field by getting its text property. Same you can do for text view.

textField.text 
textView.text

So now when button is pressed, get input from text field and if key for text field's text exists, change text of text view to value for this key

@IBAction func buttonPressed(_ sender: UIButton) {
    if let synonyms = synonyms[textField.text!] {
        let text = array.joined(separator: ", ")
        textView.text = text
    }
}

Have you noticed this line:

let text = array.joined(separator: ", ")

... you can’t set text of textView just as your array, since your array is of type [String], but text property accpets single string. So, this "joins" your array to single String with separator between each element from array.

Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
0

I hope this helps you. To return an array:

let synonyms = ["swift" : ["abrupt", "expeditious", "hasty", "nimble", "quick", "rapid", "speedy", "sudden", "unexpected"]]

    func search(key: String)-> [String] {
        //The default value should also be an array with one String element
        return synonyms[key] ?? ["I'm sorry- this word is not in the Thesaurus."] 
    }

    //This is the button action that will call the search(key:) method
    @IBAction buttonPressed(_ sender: UIButton) {
        let results = search(key: textField.text ?? "") //get the text from the UITextField
    }
meow2x
  • 2,056
  • 22
  • 27