Accessing identifier of default unwind segues in Swift leads to exception

Originator:matthias.bauch
Number:rdar://18332749 Date Originated:15-Sep-2014 01:16 AM
Status:Open Resolved:Duplicate of 18213186 (Open)
Product:iOS SDK Product Version:8.0
Classification:Serious Bug Reproducible:Always
 
Summary:
By default Xcode creates unwind segues without an identifier. The identifier does not exist, it is not an empty string. (Which would fix the problem)

It looks like this in a storyboard: 
<segue destination="foo" kind="unwind" unwindAction="unwind:" id="bar"/>

When using Objective-C this is not a problem, the identifier is nil if you access it. 
Swift declares the identifier as non-optional. So when you try to access it the app will crash. 


This poses a serious problem if you want to use an unwind segue from a view controller that implements prepareForSegue. 

The workaround is easy. In Xcode put a character into the identifier field of the unwind segue and remove it. The identifier of the segue will be an empty string, which is not nil. 

This looks like this in storyboard:

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

Steps to Reproduce:
1. run attached project. 
2. push view controller
3. try non crashing unwind segue (the workaround was applied)
4. push view controller
5. try crashing unwind segue (the default that was created by Xcode)



Expected Results:
Not crashing

Actual Results:
Crashing.

Version:
iOS 8 GM + Xcode 6.0 (6A313)

Notes:
As mentioned, the solution is trivial. Attach an empty string segue identifier when a unwind segue is created. 

Configuration:
iOS Simulator etc

Attachments:
'UnwindBugDemo.zip' was successfully uploaded.

Comments

Just hit this problem on an exit segue of my own, crashing when a prepareForSegue checks the identifier. Thank you for the workaround of just naming my exit segue.

Seems like identifier ought to be a full-blown optional, right?

By invalidname at Sept. 26, 2014, 3:58 p.m. (reply...)

I copied the original bug report. Since I can't attach the project, here are the steps to recreate it without the sample.

  1. Create Master-Detail App. iPhone, Swift

  2. Add following code to MasterViewController.swift @IBAction func unwindToMainScreen(segue: UIStoryboardSegue) { }

  3. Add following code to DetailViewController.swift override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "Foo" {

    }
    

    }

  4. In the storyboard add a button to detail vc and create an unwind segue to unwindToMainScreen. Don't touch the identifier

  5. Run, add an object so you can go to detail vc, go to detail and tap unwind button

It should crash

By matthias.bauch at Sept. 14, 2014, 11:26 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!