我想我在从处理程序操作中访问 UIAlertController 的 textField 时遇到问题。我已经用谷歌搜索了这个错误,但并没有从中得到太多。我将 Swift 与 Xcode 6.0.1 一起使用。这是错误:
'[AnyObject]?' does not have a member named 'subscript'
@IBAction func addPressed(sender: UIBarButtonItem) {
var alert = UIAlertController(title: "New Event", message: "Name of the event?", preferredStyle: .ActionSheet)
alert.addTextFieldWithConfigurationHandler(){
textField in
textField.placeholder = "Christmas"
textField.becomeFirstResponder()
}
alert.addAction(UIAlertAction(title: "Save", style: .Default, handler: {
action in
var text = alert.textFields[0].text // <-- cant access alert? The exclamation mark appears here
}))
}
使用((alert.textFields[0] as UITextField).text)
给了我完全相同的错误。