gpt4 book ai didi

swift - 用户界面文本字段? XCode 7 更新后不可转换为 'UITextField'

转载 作者:可可西里 更新时间:2023-11-01 01:38:49 25 4
gpt4 key购买 nike

更新到 XCode 7 后,我现在收到此错误“UITextField? is not convertible to '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()
}
}
})

最佳答案

正确解包您的可选内容。它将修复大部分这些错误。 textfieldsquery 都将在 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 - 用户界面文本字段? XCode 7 更新后不可转换为 'UITextField',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com