【发布时间】:2021-05-06 09:36:01
【问题描述】:
我对 swift 完全陌生,并且遵循有关创建待办事项列表应用程序的教程。只要我在self.createItem(name: text 中添加问号,就会收到错误Cannot use optional chaining on non-optional value of type 'ViewController'
@objc private func didTapAdd() {
let alert = UIAlertController(title: "New Item", message: "Enter new Item", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
alert.addAction(UIAlertAction(title: "Submit", style: .cancel, handler: {
_ in
guard let field = alert.textFields?.first, let text = field.text, !text.isEmpty else {
return
}
self?.createItem(name: text)
}))
present(alert, animated: true)
}
【问题讨论】:
-
删除“?”来自
self?.createItem(name: text)。它看起来像:self.createItem(name: text) -
阅读更多关于 swift 中的弱自我