1

When I migrate a Swift 3 app to the 4 Xcode asked me to add @objc in front of a couple of my function. And when I did it show me a warning that "@objc is deprecated" I was wondering if there is any better solution for handling addTarget in Swift 4?

enter image description here

Here is my UIButton :

let playButton: UIButton = {
    let btn = UIButton(type: .system)
    btn.setTitle("Play", for: .normal)
    btn.setTitleColor(.green, for: .normal)
    btn.addTarget(self, action: #selector(playTapped), for: .touchUpInside)
    return btn
}()

func :

@objc func playTapped() {
        let path = AudioPlayerManager.shared.audioFileInUserDocuments(name: "test")
    AudioPlayerManager.shared.playAudio(path: path)
}

it possible to add action to the button without using #selector and `@objc'?

Thanks.

sCha
  • 1,454
  • 1
  • 12
  • 22
Dark star
  • 5,192
  • 9
  • 35
  • 54
  • There are more posts discussing this issue, and all of them end with the solution that will disable deprecated warnings. However I too wonder if there's a new, Swift 4, way to do this without using `@objc` since the `xcode` says it has been deprecated. – sCha Sep 20 '17 at 04:56
  • I remember reading about this, but I'm quite rusty. What happens if you just remove the `@objc` part ? – Nicolas Miari Sep 20 '17 at 04:56
  • have you checked this answer: https://stackoverflow.com/questions/45784758/programming-a-uibutton-is-not-working-in-swift-4-xcode-9-beta-6-cannot-add-targ#45795626 – Muhammad Umair Sep 20 '17 at 04:59
  • The warning does *not* say that `@objc` is deprecated; it says that Swift 3 `@objc` *inference* is deprecated. – Hamish Sep 20 '17 at 08:48

1 Answers1

-1

Try following instruction

as use of swift 3 @objc interface in swift 4 is deprecated

enter image description here

arpita
  • 175
  • 9
  • 4
    That's a screenshot from http://evgenii.com/blog/disabling-swift3-objc-inference-in-xcode9/ without attribution, and the same solution as proposed here: https://stackoverflow.com/a/44380886/1187415. – Martin R Sep 20 '17 at 05:17