-1

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

    let translationInContainerView = gesture.translation(in: transitionContainerView!)

Occasionally crash,not every time.

I want to catch this exception,in the case of such a situation(found nil),don't want the program to crash,who can tell me how to solve this problem.

enter image description here

I try this,but no use: enter image description here

J.Doe
  • 86
  • 1
  • 5
  • you're using the bang operator (!) to unwrap "transitionContainerView", which is an optional. If you use ! and the value of that object is nil, your app will crash. That's what looks like is occurring here. Use a guard let or if let statement when creating the "transitionContainerView" object in case it is nil. If it is, that's another problem you can handle accordingly. – user3353890 Nov 25 '16 at 05:33

1 Answers1

1

Try this:

guard let transitionContainerView = self.transitionContext?.containerView else {
    print("the containerView of transitionContext is nil. Find out why.")
    return
}

You might need to cast the transitionContainerView object with a specific type. I don't know what the type is supposed to be but it would look something like this:

guard let transitionContainerView = self.transitionContext?.containerView as? MyValueType else {
    print("the containerView of transitionContext is nil. Find out why.")
    return
} 

The addition here is the as? MyValueType portion

If you don't hit the code block within the else statement, then it is not nil and you should be able to use your object accordingly. I hope this helps!

user3353890
  • 1,844
  • 1
  • 16
  • 31