我一直在关注Apple教程,开始使用Swift:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html#//apple_ref/doc/uid/TP40015214-CH22-SW1
我做的几乎是同样的事情,但出于某种原因articleLink.delegate = self
给了我
Thread 1: exc_bad_instruction (code=exc_i386_invop subcode=0x0)
和
fatal error: unexpectedly found nil while unwrapping an Optional value
我真的很困惑为什么会这样,因为我真的一步一步地遵循Apple教程这一部分......
import UIKit
import Alamofire
class ViewController: UIViewController, UITextFieldDelegate {
// MARK: Properties
@IBOutlet weak var articleLink: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field's user input through delegate callbacks
articleLink.delegate = self // ERROR OCCURS HERE
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// Hide keyboard
textField.resignFirstResponder()
return true
}
// MARK: Actions
@IBAction func submitLink(sender: AnyObject) {
}
}
答案 0 :(得分:2)
articleLink
是一个隐式解包的可选UITextField
,你指示编译器永远不会为nil。 articleLink.delegate =
尝试访问该属性以设置其委托并遇到一个nil可选项,您承诺永远不会发生这种情况。
由于此属性为IBOutlet
,我听起来在配置nib文件时无法将视图连接到此插座。如果你这样做了,那么在调用viewDidLoad
之前就已经设置了属性。