0

I have code that stopped working when I switched from Swift 3.2 to Swift 4.0. It's goal is to add click action to all UIButtons and UISwitches.

private func turnSound(state: Bool) {
    UIButton.appearance().isSoundEnabled = state
    UISwitch.appearance().isSoundEnabled = state
}
extension UIControl {
   var isSoundEnabled: Bool {
        get {
            return target(forAction: #selector(AudioManager.playStandardClickSound), withSender: nil) != nil ? true : false
        }
        set {
            if newValue {
                addTarget(AudioManager.manager, action: #selector(AudioManager.playStandardClickSound), for: .touchUpInside)
            } else {
                removeTarget(AudioManager.manager, action: #selector(AudioManager.playStandardClickSound), for: .touchUpInside)
            }
        }
    }
}

It worked well before, but now I'm getting an exception:

'NSInvalidArgumentException', reason: '*** Illegal axis type, : for appearance setter, addTarget:action:forControlEvents:. Expected NSInteger or NSUInteger'

And I don't understand how to get rid of this problem.

Will be glad to any help or alternative solution.

UPDATE:

Lypyrhythm
  • 324
  • 2
  • 13
Kirow
  • 1,077
  • 12
  • 25

1 Answers1

1

You need to add @objc to your extension too:

@objc extension UIControl

P.S. Credits goes to this answer https://stackoverflow.com/a/44391389/2241008

Anton Malmygin
  • 3,406
  • 2
  • 25
  • 31