我想获取从文本字段传递的单词的含义。 URL 如下所示:https://api.dictionaryapi.dev/api/v2/entries/en/word。这些值采用 json 格式。如何传递textField.text而不是word参数?这是代码:
struct URLQuery {
static func buildURL(baseURL: String, params: [String: String]) -> String? {
guard var components = URLComponents(string: baseURL) else {
return nil
}
let queryItems = params.map { element in
URLQueryItem(name: element.key, value: element.value)
}
if var componentsQueryItems = components.queryItems {
componentsQueryItems += queryItems
components.queryItems = componentsQueryItems
} else {
components.queryItems = queryItems
}
return components.url?.absoluteString
}
guard let word = wordTextField.text,
let urlString = URLQuery.buildURL(baseURL: "https://api.dictionaryapi.dev", params: [
"api":"v2",
"entries":"en",
"<word>": word]),
let url = URL(string: urlString)