Post

Replies

Boosts

Views

Activity

Is this a bug? UIControl.allTargets crashes when target isn't Hashable
I've got some 3rd party code that's adding a target to a UIControl with a class that is not Hashable. This seems legal since UIControl.addTarget form that takes a selector only requires that the target is Any? The code works fine (the target is called whenever it's needed) However, if something ever tries to get the UIControl instance's .allTargets property, the app crashes when trying to cast the target class (that's not hashable) into a Hashable. The error is: Could not cast value of type 'class_that_is_not_hashable' (0x.....) to 'Swift.AnyHashable' (0x....) An example is shown below. If it were my code, I'd just make the target class be Hashable, however it's not, and the class is internal final so i'm kind of stuck. Is this a bug? Or is there an implicit "the target must be NSObject" or something that I'm missing? Example follows. Also a full-fledged project is at https://github.com/SuperTango/UIControlNotHashableTargetCrash . Tap on the top button to print a log message. Tap on the bottom button to make it go boom. class ButtonHandler {     @objc func buttonTapped(_ sender: Any) {         NSLog("Print Something Button Tapped")     } } class ViewController: UIViewController {     @IBOutlet weak var button1: UIButton!     private let handler = ButtonHandler()     override func viewDidLoad() {         super.viewDidLoad()         self.printSomethingButton.addTarget(handler, action: #selector(handler.buttonTapped(_:)), for: UIControl.Event.touchUpInside)     } 		@IBAction func crashMeTapped(_ sender: Any) { 				/* This will crash. */         let targets = self.printSomethingButton.allTargets     }
1
0
645
Jul ’20