更新到 XCode 7
后,我收到此错误“UITextField
? 无法转换为 'UITextField
”。我很困惑为什么它突然无法转换。有什么想法吗?
let saveAction = UIAlertAction(title: "Search",
style: UIAlertActionStyle.Default,
handler: {(action) in
let query =
(alertController.textFields?[0] as UITextField).text ### errors here
let tag = query
if !query.isEmpty {
if self.model.queryForTag(tag) != nil {
}
else if isNew {
self.model.saveQuery(
query, forTag: tag, syncToCloud: true)
let indexPath =
NSIndexPath(forRow: 0, inSection: 0)
self.tableView.insertRowsAtIndexPaths([indexPath],
withRowAnimation: .Automatic)
self.forceSearch(indexPath)
self.tableView.reloadData()
}
}
})
最佳答案
正确打开您的选项。它将修复大部分错误。 文本字段
和查询
都将在guard语句之后可用
let saveAction = UIAlertAction(title: "Search",
style: UIAlertActionStyle.Default,
handler: {(action) in
guard let textFields = alertController.textFields, let first = textFields.first, let query = first.text else {
return
}
})
附: guard 声明也将有助于您不断增长的金字塔。
关于swift - UI 文本字段? XCode 7 更新后无法转换为 'UITextField',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816045/