Hello,
somehow today I stumbled upon a strange behaviour in Swift.
I am using an extension for a UIViewController which contains a class function:
extension SettingsViewController {
@objc class func shakeSwitchChanged(switchState: UISwitch) {
let userDefaults = UserDefaults.standard
userDefaults.set(switchState.isOn, forKey: PokerUserDefaultKeys.FlipOnShake.rawValue)
}
}In the view I am using a UISwitch and add a target to it in this way:
flipSwitch?.addTarget(self, action: #selector(SettingsViewController.shakeSwitchChanged(switchState:)), for: .valueChanged)In Xcode 10 this code comiles fine (no errors, no warnings), but during runtime the app crashes when flipping the switch by saying:
2018-10-12 12:58:53.649412+0200 bridgingIT Planning Poker[30655:4166508] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[bridgingIT_Planning_Poker.SettingsViewController shakeSwitchChangedWithSwitchState:]: unrecognized selector sent to instance 0x7fdb48e14b00'
Putting the function into the class itself, does not work either.
When i change the function definition to be an instance function (i.e. removing "class") everything works fine.
It is also very strange that if I define the function as an instance function of the class, everything works although the selector remains the same (#selector(SettingsViewController.shakeSwitchChanged(switchState:)). I expected at least compile-time errors or warnings, but nothing. Even the app runs fine with this...
Am I doing something wrong or is it possibly a bug in Swift 4.2?
Thanks in advance and best regards
Martin