设置delegate = self导致线程1:exc_bad_instruction(code = exc_i386_invop subcode = 0x0)

时间:2016-03-21 02:47:08

标签: ios swift wrapping demo

我一直在关注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) {
    }
}

1 个答案:

答案 0 :(得分:2)

articleLink是一个隐式解包的可选UITextField,你指示编译器永远不会为nil。 articleLink.delegate =尝试访问该属性以设置其委托并遇到一个nil可选项,您承诺永远不会发生这种情况。

由于此属性为IBOutlet,我听起来在配置nib文件时无法将视图连接到此插座。如果你这样做了,那么在调用viewDidLoad之前就已经设置了属性。