If someone can help me understand/fix why it keeps crashing/saying my results is nil when there's an actual string being sent.
I know the problem has to do with white space but what am I doing wrong?
fatal error: unexpectedly found nil while unwrapping an Optional value
import Alamofire
import SwiftyJSON
@IBAction func searchBtn(sender: UIButton) {
let searchTerm:String = self.searchTextField.text!
let apiUrl = "http://localhost/v1/define?term=\(searchTerm)"
Alamofire.request(.GET, apiUrl!).responseJSON { response in print(response)
let jsonData = JSON(response.result.value!)
if jsonData["result_type"] == "exact" {
let definitionText = jsonData["list"][0]["definition"].string
self.resultLabel.text = definitionText
} else {
self.resultLabel.text = "Sorry, your word does not have a definition."
}
}
}
I'm using this for URL encode in swift 2, hope it work
let escapedSearchText = searchText.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.letterCharacterSet())!