xcode6 beta7 prepareForSegue throws EXC_BAD_ACCESS
Asked Answered
S

1

10

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!

Sutlej answered 5/9, 2014 at 8:17 Comment(6)
Just a thought: Did you clean your build folder after upgrading to Beta 7?Titrant
Mmm.. no luck with the clean either. (good reminder though - that caught me out with a few of the past betas also :) )Sutlej
I had a similar problem with a segue. Try this: for every segue in Interface Builder that you've got that doesn't have an identifier, type something in, remove it, then hit return. Then rebuild and try it again. I had a segue with no identifier that I'd added in a previous beta, and it was that that was causing the problem. By forcing it to be re-set to no identifier again in the latest Xcode, I guess I changed something in the xib that needed to exactly match some runtime code in the latest beta.Collation
Thanks @MattGibson - Tried what you suggested, but still no luck...I'll try to recreate the issue with a test project and see if that fixes it.Sutlej
I'm still seeing the same problem in Xcode 6 GM release - @MattGibson you're method seemed to work for me!Pero
You may want to quickly try deleting the project's "derived data" and rebuilding, if you haven't already (you can do that from the Organizer). (Also, try resetting all the segues that do have an identifier—just re-type their IDs and hit return.)Collation
B
14

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

Bison answered 14/9, 2014 at 23:35 Comment(2)
Cool, glad I was pointing in the right direction. Thanks for digging deeper into what's going on behind the scenes.Collation
To add to this, the segue that I was using wasn't the problem as it had an identifier defined. My problem was another segue in the storyboard didn't have an identifier.Calmative

© 2022 - 2024 — McMap. All rights reserved.