Xcode 8 / Swift 2.3 connecting buttons to code via Interface Builder can result in crash

Originator:rwgrier
Number:rdar://28115553 Date Originated:01-Sep-2016 10:58 AM
Status:Duplicate/27410040 Resolved:08-Sep-2016 02:27 AM
Product:Developer Tools Product Version:8.0 beta 6 (8S201h)
Classification:Serious Bug Reproducible:Always
 
Summary:
If you have a project in Xcode 8 using Swift 2.3 and connect a button from a storyboard to code via Interface Builder, it will result in a crash at runtime. This doesn't happen if you're using Swift 3.0. 

I've attached a sample project which demonstrates this.

Steps to Reproduce:
1. In an existing Storyboard, add a new button to a scene.
2. In an existing view controller (the one from the Storyboard scene), add a method (like: `@IBAction func buttonPressViaInterfaceBuilder(sender: AnyObject) {`).
3. Use Interface Builder to connect the button's action to the newly created method. 
4. Run the application. 
5. Navigate to the scene.
6. Press the button to trigger the action. 

Expected Results:
I would expect the method connected to the action to trigger without issue. 

Actual Results:
Instead, I get a crash and this stack trace:

2016-09-01 10:33:24.803 BrokenIBMapping[10430:142747] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BrokenIBMapping.ViewController buttonPressViaInterfaceBuilderWithSender:]: unrecognized selector sent to instance 0x7fc8db003ea0'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010300634b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000010069421e objc_exception_throw + 48
	2   CoreFoundation                      0x0000000103075f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
	3   CoreFoundation                      0x0000000102f8bc15 ___forwarding___ + 1013
	4   CoreFoundation                      0x0000000102f8b798 _CF_forwarding_prep_0 + 120
	5   UIKit                               0x0000000100b656c8 -[UIApplication sendAction:to:from:forEvent:] + 83
	6   UIKit                               0x0000000100ce909a -[UIControl sendAction:to:forEvent:] + 67
	7   UIKit                               0x0000000100ce93b3 -[UIControl _sendActionsForEvents:withEvent:] + 444
	8   UIKit                               0x0000000100ce82af -[UIControl touchesEnded:withEvent:] + 668
	9   UIKit                               0x0000000100bd2875 -[UIWindow _sendTouchesForEvent:] + 2747
	10  UIKit                               0x0000000100bd3f63 -[UIWindow sendEvent:] + 4011
	11  UIKit                               0x0000000100b811e3 -[UIApplication sendEvent:] + 371
	12  UIKit                               0x000000010135b06f __dispatchPreprocessedEventFromEventQueue + 3248
	13  UIKit                               0x0000000101353d19 __handleEventQueue + 4879
	14  CoreFoundation                      0x0000000102fab311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	15  CoreFoundation                      0x0000000102f9059c __CFRunLoopDoSources0 + 556
	16  CoreFoundation                      0x0000000102f8fa86 __CFRunLoopRun + 918
	17  CoreFoundation                      0x0000000102f8f494 CFRunLoopRunSpecific + 420
	18  GraphicsServices                    0x0000000104eaea6f GSEventRunModal + 161
	19  UIKit                               0x0000000100b63a74 UIApplicationMain + 159
	20  BrokenIBMapping                     0x00000001000bb522 main + 114
	21  libdyld.dylib                       0x0000000103f3068d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException


Version:
Version 8.0 beta 6 (8S201h)

Notes:
I did notice that Interface Builder renames the method that I'm trying to hook up to `buttonPressViaInterfaceBuilderWithSender` this may be the root of the issue. 

Work around #1: If I set the action via code, the button responds as expected and without crashing. 

Work around #2: If I use `_` as the parameter name instead of `sender`, Interface Builder won't rename the method (in Interface Builder), and it triggers the method without crashing. 

Configuration:


Attachments:
'BrokenIBMapping.zip' was successfully uploaded.

Github Repo: https://github.com/rwgrier/radar-28115553

Comments

This still happening in the public release of Xcode Version 8.0 (8A218a)

By alessandro.calzavara at Sept. 19, 2016, 1:02 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!