Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xcode6 beta7 prepareForSegue throws EXC_BAD_ACCESS

I've just installed XCode6 Beta-7 and am now seeing an access exception on one of my PrepareForSegue methods - (called when a Modal Segue is about to unwind)

The code in question looks like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {


    if (segue.identifier == "MY_IDENTIFIER") { //EXC_BAD_ACCESS (code=1, address=0x0)
        //Never gets here...
    }


}

I've tried making the segue parameter an optional but as far as Swift is concerned, segue is not nil, so even with a check like the below, I have the same failure...

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject!) {

    if (segue != nil) 
       if (segue!.identifier == "MY_IDENTIFIER") { //EXC_BAD_ACCESS (code=1, address=0x0)
           //Never gets here...
       }
    }


}

All other segues in the application seem to work fine, but this one is failing - and it seems to occur only in the case of an unwind being issued. Anyone else encountered this?

EDIT / Workaround

A simple workaround is to avoid using the unwindSegue method and simply call dismissViewControllerAnimated , but I'd still love to know why the unwindSegue method is failing in this instance...

Many thanks!

like image 726
Nash Avatar asked Sep 05 '14 08:09

Nash


1 Answers

As Matt Gibson figured out adding and removing the segue identifier fixes the problem.

The reason for the bug is that Xcode by default does not add an identifier for unwind segues.

The default unwind segue in the storyboard looks like this:

<segue destination="foo" kind="unwind" unwindAction="unwind:" id="bar"/>

In Objective-C this was not a problem, segue.identifier would be nil. In Swift identifier is declared as String, a non-optional string. But the identifier is still nil in the storyboard, so the SDK returns nil where it said it does return a non-optional string. This crashes at runtime.

Once you have changed and removed the identifier in the storyboard the identifier will be "", an empty string.

<segue destination="foo" kind="unwind" identifier="" unwindAction="unwind:" id="bar"/>

Which of course fixes the problem, because an empty string matches the specified return value of the identifier getter.

I filed a radar for this. You should dupe it in Apples Bug Reporter

like image 149
Matthias Bauch Avatar answered Nov 18 '22 01:11

Matthias Bauch