用户界面文本字段? XCode 7 更新后无法转换为 'UITextField'
UITextField? is not convertible to 'UITextField' after XCode 7 update
更新到 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()
}
}
})
正确展开您的可选商品。它将修复大部分这些错误。 textfields
和 query
都将在 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
}
})
p.s。 guard 语句也将有助于您不断增长的金字塔。
更新到 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()
}
}
})
正确展开您的可选商品。它将修复大部分这些错误。 textfields
和 query
都将在 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
}
})
p.s。 guard 语句也将有助于您不断增长的金字塔。