iOS fatal error: unexpectedly found nil while unwrapping an Optional value

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
280
86
Hi

I'm trying to add 3d touch quick actions to an app but i keep on getting "fatal error: unexpectedly found nil while unwrapping an Optional value" whenever i try to edit a UITextfield or a UIButton using the quick action list, the app crash on launch

heres the code using Xcode Version 7.0.1 (7A1001) with swift

Code:
import UIKit

@available(iOS 9.0, *)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
   
    @IBOutlet weak var Button: UIButton!
    @IBOutlet weak var Field: UITextField!
   
    var window: UIWindow?
    var shortcutItem: UIApplicationShortcutItem?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        var performShortcutDelegate = true
        if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
            self.shortcutItem = shortcutItem
            performShortcutDelegate = false
        }
        return performShortcutDelegate
    }
   
    func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
        completionHandler( handleShortcut(shortcutItem) )
    }

    func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
        var succeeded = false
        if( shortcutItem.type == "First" ) {
            succeeded = true
            Button.setTitle("whatever", forState: UIControlState.Normal) //the error
            Field.text = "some text"    //the error   
            //whatever variable outlet i try change gives error there
        }
        if( shortcutItem.type == "Second" ) {
            succeeded = true
            // haven't added anything there yet so app launch normally and does nothing
        }
        if( shortcutItem.type == "Third" ) {
            succeeded = true
        }
        if( shortcutItem.type == "Fourth" ) {
            succeeded = true
        }
        return succeeded
    }

    func applicationDidBecomeActive(application: UIApplication) {
        guard let shortcut = shortcutItem else { return }
       
        handleShortcut(shortcut)
        self.shortcutItem = nil
    }
}
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
var succeeded = false
if( shortcutItem.type == "First" ) {
succeeded = true
Button.setTitle("whatever", forState: UIControlState.Normal)
//the error
Field.text = "some text" //the error
//whatever variable outlet i try change gives error there
}
if( shortcutItem.type == "Second" ) {
succeeded = true
// haven't added anything there yet so app launch normally and does nothing
}
if( shortcutItem.type == "Third" ) {
succeeded = true
}
if( shortcutItem.type == "Fourth" ) {
succeeded = true
}
return succeeded
}
code source
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
These crashes suggest that your IBOutlets aren't hooked up.

In the storyboard if you right-click or control-click on the button or text field you should see a little window that shows the connections. Make sure everything looks correct there.

Alternatively it could be that textfield.text is returning nil and you're not unwrapping it correctly.
 

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
280
86
These crashes suggest that your IBOutlets aren't hooked up.

In the storyboard if you right-click or control-click on the button or text field you should see a little window that shows the connections. Make sure everything looks correct there.

Alternatively it could be that textfield.text is returning nil and you're not unwrapping it correctly.
they are connected correctly, i tried changing the value using a button connected to an ibaction and it worked. but as soon as i try editing it using the quick action it ends up crashing the app, also tried the following instead of editing the field directly from the quick action and after 5 seconds the app crashed even though before that, i was able to edit the textfield through an ibaction

Code:
var item = ""
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
       var succeeded = false
        if( shortcutItem.type == "First" ) {
            item = shortcutItem.type
           succeeded = true
            self.performSelector("Action", withObject: nil, afterDelay: 5)
        }
       if( shortcutItem.type == "Second" ) {
            item = shortcutItem.type
           succeeded = true
       }
       if( shortcutItem.type == "Third" ) {
            item = shortcutItem.type
           succeeded = true
       }
       if( shortcutItem.type == "Fourth" ) {
            item = shortcutItem.type
           succeeded = true
       }
       return succeeded
   }

func Action(){
        if item == "First"{
            Field.text = "sometext"
        }
        if item == "Second"{           
        }
        if item == "Third"{           
        }
        if item == "Fourth"{         
        }
    }
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
OK, I spent a little time looking at Apple's sample code for shortcut items and some stack overflow comments. While it is possible that your IBOutlets are hooked up correctly it's also possible that they don't yet exist when you're launching so they haven't been connected to your app delegate yet. I realize that your attempt to use performSelector:afterDelay: is an attempt to avoid this problem.

Let me say that it's unusual to have outlets to views in your app delegate. One would normally have such outlets in the view controller for the scene that holds these objects. I would recommend that instead of trying to set these values directly from your app delegate you instead add methods to the view controller that handles these things. You can be more certain that the view controller exists and its views exist before they're manipulated. Those crashes are because either the views don't exist yet or because the outlets aren't hooked up correctly. That's why they're nil.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.