performSelector crash?

Hi,


I'd like to programmatically tap a navigation bar button, so I wrote some code like this in a view controller:


let b = navigationItem.rightBarButtonItem
if let t = b?.target, let a = b?.action {
    print("trying to perform selector")
    t.performSelector(a)
}


(I know, normally you should directly call the target, but this is an odd situation. [1])


I get a crash: EXC_BAD_ACCESS. The stack trace in debugger shows:


0 swift_unknownRetain

1 @objc MasterViewController.insertNewObject

2. MasterViewController.viewDidAppear


#1 is the expected method for the selector, but it's body is never executed. What is `unknownRetain`? Is `performSelector` not usable from Swift?


Rob


[1]: http://stackoverflow.com/questions/10426622/uisplitviewcontroller-how-force-to-show-master-popover-in-app-launch-portrait

Answered by OOPer in 92834022

I'm not sure this would resolve your issue, but performSelector easily causes runtime crash even in Objective-C, if you do not supply proper arguments.

What will happen if you change the code as follows?

        t.performSelector(a, withObject: self)
Accepted Answer

I'm not sure this would resolve your issue, but performSelector easily causes runtime crash even in Objective-C, if you do not supply proper arguments.

What will happen if you change the code as follows?

        t.performSelector(a, withObject: self)

[Sigh]... I should have retired from this profession a long time ago. Thanks.

performSelector crash?
 
 
Q