Unexpectedly found nil while implicitly unwrapping an Optional value

Hey,all


I tried to initialize a viewcontroller in another viewcontroller.


this is a part of my code:




class FirstViewController: UIViewController, CircleMenuDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {


@IBAction func to2(_ sender: UIButton) {

let sb = UIStoryboard(name:"Main",bundle: Bundle.main)

let view2 = sb.instantiateViewController(withIdentifier: "view2")

layerTransition(animTye: .cube, subType: .ramdom, curve: .EaseInEaseOut, duration: 0.4, layer: (self.view.window?.layer)!)

self.present(view2,animated: true,completion: nil)

}



class SecondViewController: UIViewController {


typealias FilteringCompletion = ((UIImage?, Error?) -> ())


@IBOutlet private var imageView: UIImageView!

@IBOutlet private var loader: UIActivityIndicatorView!

@IBOutlet private var buttonHolderView: UIView!

@IBOutlet private var applyButton: UIButton!

@IBOutlet private var loaderWidthConstraint: NSLayoutConstraint!


var imagePicker = UIImagePickerController()

var isProcessing : Bool = false {

didSet {

self.applyButton.isEnabled = !isProcessing

self.isProcessing ? self.loader.startAnimating() : self.loader.stopAnimating()

self.loaderWidthConstraint.constant = self.isProcessing ? 20.0 : 0.0

UIView.animate(withDuration: 0.3) {

self.isProcessing

? self.applyButton.setTitle("processing", for: .normal)

: self.applyButton.setTitle("process", for: .normal)

self.view.layoutIfNeeded()

}

}

}



When the app just runs,it crashes.

I got an error at "self.applyButton.isEnabled = !isProcessing"

and the error message is "Unexpectedly found nil while implicitly unwrapping an Optional value"


Can someone please explain this, or at least, tell me what I need to write to get it correct?


All suggestions would be great!


Thanks.

Just put `print(self.applyButton)` before the line `self.applyButton.isEnabled = !isProcessing` and see what is printed.

(Ignore some compiler warnings.)

What it printed are as follows:



nil

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

2019-04-06 12:09:35.513766+0800 IRIS iOS[82036:2615897] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

it printed:

nil

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

2019-04-06 12:09:35.513766+0800 IRIS iOS[82036:2615897] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value


I reconnected the applyButton to the button in storyboard,and the error disappears.But when I run this project,It crashed in AppDelegate.swift at "class AppDelegate: UIResponder, UIApplicationDelegate"

error message :


Optional(<UIButton: 0x7ff76e51d090; frame = (0 0; 49 42); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6000008b5140>>)

2019-04-06 13:33:16.057133+0800 IRIS iOS[86863:2645289] -[UIView setConstant:]: unrecognized selector sent to instance 0x7ff76e700a50

2019-04-06 13:33:16.063175+0800 IRIS iOS[86863:2645289] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setConstant:]: unrecognized selector sent to instance 0x7ff76e700a50'

*** First throw call stack:

(

0 CoreFoundation 0x00000001089b46fb __exceptionPreprocess + 331

1 libobjc.A.dylib 0x0000000107f58ac5 objc_exception_throw + 48

2 CoreFoundation 0x00000001089d2ab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132

3 UIKitCore 0x0000000113e42c3d -[UIResponder doesNotRecognizeSelector:] + 287

4 CoreFoundation 0x00000001089b9443 ___forwarding___ + 1443

5 CoreFoundation 0x00000001089bb238 _CF_forwarding_prep_0 + 120

6 IRIS iOS 0x00000001074f2f1a $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvW + 1418

7 IRIS iOS 0x00000001074f34c2 $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvs + 274

8 IRIS iOS 0x00000001074f3637 $s8IRIS_iOS20SecondViewControllerC11viewDidLoadyyF + 151

9 IRIS iOS 0x00000001074f3844 $s8IRIS_iOS20SecondViewControllerC11viewDidLoadyyFTo + 36

10 UIKitCore 0x000000011381443b -[UIViewController loadViewIfRequired] + 1183

11 UIKitCore 0x0000000113814868 -[UIViewController view] + 27

12 UIKitCore 0x0000000113e4cc33 -[UIWindow addRootViewControllerViewIfPossible] + 122

13 UIKitCore 0x0000000113e4d327 -[UIWindow _setHidden:forced:] + 289

14 UIKitCore 0x0000000113e5ff86 -[UIWindow makeKeyAndVisible] + 42

15 UIKitCore 0x0000000113e0ff1c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4555

16 UIKitCore 0x0000000113e150c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617

17 UIKitCore 0x000000011365a6d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904

18 UIKitCore 0x0000000113662fce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153

19 UIKitCore 0x000000011365a2ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236

20 UIKitCore 0x000000011365ac48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091

21 UIKitCore 0x0000000113658fba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782

22 UIKitCore 0x0000000113658c71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433

23 UIKitCore 0x000000011365d9b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576

24 UIKitCore 0x000000011365e610 _performActionsWithDelayForTransitionContext + 100

25 UIKitCore 0x000000011365d71d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223

26 UIKitCore 0x00000001136626d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392

27 UIKitCore 0x0000000113e139a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514

28 UIKitCore 0x00000001139cadfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361

29 FrontBoardServices 0x0000000112b4b125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448

30 FrontBoardServices 0x0000000112b54ed6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283

31 FrontBoardServices 0x0000000112b54700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53

32 libdispatch.dylib 0x000000010c77adb5 _dispatch_client_callout + 8

33 libdispatch.dylib 0x000000010c77e2ba _dispatch_block_invoke_direct + 300

34 FrontBoardServices 0x0000000112b86146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30

35 FrontBoardServices 0x0000000112b85dfe -[FBSSerialQueue _performNext] + 451

36 FrontBoardServices 0x0000000112b86393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42

37 CoreFoundation 0x000000010891bbe1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17

38 CoreFoundation 0x000000010891b463 __CFRunLoopDoSources0 + 243

39 CoreFoundation 0x0000000108915b1f __CFRunLoopRun + 1231

40 CoreFoundation 0x0000000108915302 CFRunLoopRunSpecific + 626

41 GraphicsServices 0x000000010f9a12fe GSEventRunModal + 65

42 UIKitCore 0x0000000113e16ba2 UIApplicationMain + 140

43 IRIS iOS 0x000000010751735b main + 75

44 libdyld.dylib 0x000000010c7ef541 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException



What should I do next?

Most likely, you did not connect the applyButton to its IBOutlet, or the connection is corrupted.


If so, disconnect the button and connect again.

I reconnected the applyButton to the button in storyboard,and the error disappears.But when I run this project,It crashed in AppDelegate.swift at "class AppDelegate: UIResponder, UIApplicationDelegate"

error message :


Optional(<UIButton: 0x7ff76e51d090; frame = (0 0; 49 42); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6000008b5140>>)

2019-04-06 13:33:16.057133+0800 IRIS iOS[86863:2645289] -[UIView setConstant:]: unrecognized selector sent to instance 0x7ff76e700a50

2019-04-06 13:33:16.063175+0800 IRIS iOS[86863:2645289] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setConstant:]: unrecognized selector sent to instance 0x7ff76e700a50'

*** First throw call stack:

(

0 CoreFoundation 0x00000001089b46fb __exceptionPreprocess + 331

1 libobjc.A.dylib 0x0000000107f58ac5 objc_exception_throw + 48

2 CoreFoundation 0x00000001089d2ab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132

3 UIKitCore 0x0000000113e42c3d -[UIResponder doesNotRecognizeSelector:] + 287

4 CoreFoundation 0x00000001089b9443 ___forwarding___ + 1443

5 CoreFoundation 0x00000001089bb238 _CF_forwarding_prep_0 + 120

6 IRIS iOS 0x00000001074f2f1a $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvW + 1418

7 IRIS iOS 0x00000001074f34c2 $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvs + 274

8 IRIS iOS 0x00000001074f3637 $s8IRIS_iOS20SecondViewControllerC11viewDidLoadyyF + 151

9 IRIS iOS 0x00000001074f3844 $s8IRIS_iOS20SecondViewControllerC11viewDidLoadyyFTo + 36

10 UIKitCore 0x000000011381443b -[UIViewController loadViewIfRequired] + 1183

11 UIKitCore 0x0000000113814868 -[UIViewController view] + 27

12 UIKitCore 0x0000000113e4cc33 -[UIWindow addRootViewControllerViewIfPossible] + 122

13 UIKitCore 0x0000000113e4d327 -[UIWindow _setHidden:forced:] + 289

14 UIKitCore 0x0000000113e5ff86 -[UIWindow makeKeyAndVisible] + 42

15 UIKitCore 0x0000000113e0ff1c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4555

16 UIKitCore 0x0000000113e150c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617

17 UIKitCore 0x000000011365a6d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904

18 UIKitCore 0x0000000113662fce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153

19 UIKitCore 0x000000011365a2ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236

20 UIKitCore 0x000000011365ac48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091

21 UIKitCore 0x0000000113658fba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782

22 UIKitCore 0x0000000113658c71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433

23 UIKitCore 0x000000011365d9b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576

24 UIKitCore 0x000000011365e610 _performActionsWithDelayForTransitionContext + 100

25 UIKitCore 0x000000011365d71d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223

26 UIKitCore 0x00000001136626d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392

27 UIKitCore 0x0000000113e139a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514

28 UIKitCore 0x00000001139cadfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361

29 FrontBoardServices 0x0000000112b4b125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448

30 FrontBoardServices 0x0000000112b54ed6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283

31 FrontBoardServices 0x0000000112b54700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53

32 libdispatch.dylib 0x000000010c77adb5 _dispatch_client_callout + 8

33 libdispatch.dylib 0x000000010c77e2ba _dispatch_block_invoke_direct + 300

34 FrontBoardServices 0x0000000112b86146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30

35 FrontBoardServices 0x0000000112b85dfe -[FBSSerialQueue _performNext] + 451

36 FrontBoardServices 0x0000000112b86393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42

37 CoreFoundation 0x000000010891bbe1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17

38 CoreFoundation 0x000000010891b463 __CFRunLoopDoSources0 + 243

39 CoreFoundation 0x0000000108915b1f __CFRunLoopRun + 1231

40 CoreFoundation 0x0000000108915302 CFRunLoopRunSpecific + 626

41 GraphicsServices 0x000000010f9a12fe GSEventRunModal + 65

42 UIKitCore 0x0000000113e16ba2 UIApplicationMain + 140

43 IRIS iOS 0x000000010751735b main + 75

44 libdyld.dylib 0x000000010c7ef541 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException



What should I do next?

I reconnected the applyButton to the button in storyboard,and the error disappears.But when I run this project,It crashed in AppDelegate.swift at "class AppDelegate: UIResponder, UIApplicationDelegate"

error message :


Optional(<UIButton: 0x7ff76e51d090; frame = (0 0; 49 42); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x6000008b5140>>)

2019-04-06 13:33:16.057133+0800 IRIS iOS[86863:2645289] -[UIView setConstant:]: unrecognized selector sent to instance 0x7ff76e700a50

2019-04-06 13:33:16.063175+0800 IRIS iOS[86863:2645289] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setConstant:]: unrecognized selector sent to instance 0x7ff76e700a50'

*** First throw call stack:

(

0 CoreFoundation 0x00000001089b46fb __exceptionPreprocess + 331

1 libobjc.A.dylib 0x0000000107f58ac5 objc_exception_throw + 48

2 CoreFoundation 0x00000001089d2ab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132

3 UIKitCore 0x0000000113e42c3d -[UIResponder doesNotRecognizeSelector:] + 287

4 CoreFoundation 0x00000001089b9443 ___forwarding___ + 1443

5 CoreFoundation 0x00000001089bb238 _CF_forwarding_prep_0 + 120

6 IRIS iOS 0x00000001074f2f1a $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvW + 1418

7 IRIS iOS 0x00000001074f34c2 $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvs + 274

8 IRIS iOS 0x00000001074f3637 $s8IRIS_iOS20SecondViewControllerC11viewDidLoadyyF + 151

9 IRIS iOS 0x00000001074f3844 $s8IRIS_iOS20SecondViewControllerC11viewDidLoadyyFTo + 36

10 UIKitCore 0x000000011381443b -[UIViewController loadViewIfRequired] + 1183

11 UIKitCore 0x0000000113814868 -[UIViewController view] + 27

12 UIKitCore 0x0000000113e4cc33 -[UIWindow addRootViewControllerViewIfPossible] + 122

13 UIKitCore 0x0000000113e4d327 -[UIWindow _setHidden:forced:] + 289

14 UIKitCore 0x0000000113e5ff86 -[UIWindow makeKeyAndVisible] + 42

15 UIKitCore 0x0000000113e0ff1c -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4555

16 UIKitCore 0x0000000113e150c6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1617

17 UIKitCore 0x000000011365a6d6 __111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 904

18 UIKitCore 0x0000000113662fce +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153

19 UIKitCore 0x000000011365a2ec -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 236

20 UIKitCore 0x000000011365ac48 -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 1091

21 UIKitCore 0x0000000113658fba __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 782

22 UIKitCore 0x0000000113658c71 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 433

23 UIKitCore 0x000000011365d9b6 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 576

24 UIKitCore 0x000000011365e610 _performActionsWithDelayForTransitionContext + 100

25 UIKitCore 0x000000011365d71d -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 223

26 UIKitCore 0x00000001136626d0 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392

27 UIKitCore 0x0000000113e139a8 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 514

28 UIKitCore 0x00000001139cadfa -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361

29 FrontBoardServices 0x0000000112b4b125 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 448

30 FrontBoardServices 0x0000000112b54ed6 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 283

31 FrontBoardServices 0x0000000112b54700 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 53

32 libdispatch.dylib 0x000000010c77adb5 _dispatch_client_callout + 8

33 libdispatch.dylib 0x000000010c77e2ba _dispatch_block_invoke_direct + 300

34 FrontBoardServices 0x0000000112b86146 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30

35 FrontBoardServices 0x0000000112b85dfe -[FBSSerialQueue _performNext] + 451

36 FrontBoardServices 0x0000000112b86393 -[FBSSerialQueue _performNextFromRunLoopSource] + 42

37 CoreFoundation 0x000000010891bbe1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17

38 CoreFoundation 0x000000010891b463 __CFRunLoopDoSources0 + 243

39 CoreFoundation 0x0000000108915b1f __CFRunLoopRun + 1231

40 CoreFoundation 0x0000000108915302 CFRunLoopRunSpecific + 626

41 GraphicsServices 0x000000010f9a12fe GSEventRunModal + 65

42 UIKitCore 0x0000000113e16ba2 UIApplicationMain + 140

43 IRIS iOS 0x000000010751735b main + 75

44 libdyld.dylib 0x000000010c7ef541 start + 1

)

libc++abi.dylib: terminating with uncaught exception of type NSException



What should I do next?

Thanks for your updated information. And sorry that i haven't noticed that till now.


Unfortunately, crashed in AppDelegate.swift at "class AppDelegate: UIResponder, UIApplicationDelegate" does not give us much information, because Swift embeds intentional crash code near the AppDelegate.


The main reason that your app is craching is because `setCostant:` method is sent to `UIView` which does not implement a method `setConstant:`.


- Somewhere in your code, you have a line calling `setConstant:`

- Somewhere in your code, you have a line of assignment `〜.constant = ...`

- Somewhere in your storyboard settings, you may be settings the property named `constant`

- ...


And according to the stack trace, the most susupiciaous part of you code is shown as :

6 IRIS iOS 0x00000001074f2f1a $s8IRIS_iOS20SecondViewControllerC12isProcessingSbvW + 1418

(Find the first line in the stack trace which is showing your app name.)


This line suggests that somethng wrong is done in the `isProcessing` method of your class `SecondViewController`.


With two info above, we can reach at one line of your code:

            self.loaderWidthConstraint.constant = self.isProcessing ? 20.0 : 0.0


`loaderWidthConstraint` is declared as an @IBOutlet of type `NSLayoutConstraint`, so you may have connected a constraint in your storyboard to this property, but now, it is broken and is connected to some view in your storyboard.


As you have found your `applyButton` is connected to something wrong and re-connecting it fixed some issue of your project, you may need to re-check (remove connection and re-connect) the connection of `loaderWidthConstraint` whether it is connected to the right NSLayoutConstraint.


As your storyboard is sort of broken, you may need to re-check all connections.

How did you define the button action ? By connecting from IB ? By defining action in code ?


If the latter, please show the code, may be you have not defined the selector correctly (missing or extra colon (:) in the call).

Unexpectedly found nil while implicitly unwrapping an Optional value
 
 
Q