2016-03-21 105 views
0

我一直在關注蘋果教程入門斯威夫特在這裏:https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html#//apple_ref/doc/uid/TP40015214-CH22-SW1設置委託=自導致線程1:exc_bad_instruction(代碼= exc_i386_invop子碼=爲0x0)

我做幾乎同樣的事情,但對於一些原因articleLink.delegate = self是給我

Thread 1: exc_bad_instruction (code=exc_i386_invop subcode=0x0)

fatal error: unexpectedly found nil while unwrapping an Optional value

我真搞不清楚爲什麼發生這種情況,因爲我從字面上繼這部分步蘋果教程一步...

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) { 
    } 
} 

回答

2

articleLink是隱含展開可選UITextField,是指要求編譯器它永遠不會是零。 articleLink.delegate =嘗試訪問該屬性以設置其委託並遇到無可選項,您承諾的內容永遠不會發生。

由於此屬性是IBOutlet我聽起來像是在配置nib文件時未能將視圖連接到此插座。如果你這樣做了,那麼在viewDidLoad被調用之前,該屬性將被設置。

+0

謝謝!我對Swift很陌生。我應該如何解決這個問題? – user6091470

+0

這聽起來像是你在該教程中跳過了第8步的一部分,或者在創建斷開連接的插座後進行了更改。您需要檢查您的筆尖中的視圖是否仍然連接到該插座,以下是在此處的界面構建器中可以找到該示例的示例:https://developer.apple.com/library/ios/recipes/xcode_help- IB_connections /章節/ Connections.html#// apple_ref/DOC/UID/TP40014227-CH20-SW1 – Jonah

相關問題