0

I have been following the Apple tutorial to getting started with Swift here: https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson3.html#//apple_ref/doc/uid/TP40015214-CH22-SW1

I'm doing almost the same thing but for some reason articleLink.delegate = self is giving me

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

and

fatal error: unexpectedly found nil while unwrapping an Optional value

I'm really confused about why this is happening because I'm literally following the Apple tutorial step by step for this part...

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) {
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

2

articleLink is an implicitly unwrapped optional UITextField, you are instructing the compiler that it will never be nil. articleLink.delegate = attempts to access that property in order to set its delegate and encounters a nil optional, something you promised would never happen.

Since this property is an IBOutlet I sounds like you failed to connect a view to this outlet when configuring a nib file. If you had done so then the property would have been set before viewDidLoad is called.

Jonah
  • 17,918
  • 1
  • 43
  • 70
  • Thanks! I'm pretty new to Swift. How should I fix this? – user6091470 Mar 21 '16 at 14:45
  • It sounds like you either skipped part of step 8 in that tutorial or make a change after creating the outlet which broke the connection. You'll need to check if the view in your nib is still connected to that outlet, there's an example of where to find that in interface builder here: https://developer.apple.com/library/ios/recipes/xcode_help-IB_connections/chapters/Connections.html#//apple_ref/doc/uid/TP40014227-CH20-SW1 – Jonah Mar 21 '16 at 14:49