"Unrecognized Selector Sent to Instance" Error

Hi There I have the problem with this code

    private func initializer(){
    let gesture = UITapGestureRecognizer(target: self, action: Selector(("onTapLabel")))
    addGestureRecognizer(gesture)
    isUserInteractionEnabled = true

    font = UIFont(name: "icomoon", size: font.pointSize)
    if isChecked{
        text = ""
    }else{
        text = ""
    }
    
}
internal func onTapLabel(){
        print("A")
    }

I had used this code in swift 2 but now I can't use it!

Use #selector instead of Selector()

1 Like

it's shows this error
Argu ment of '#selector' does not refer to an '@objc' method, property, or initializer

Add @objc to your function, so:

@objc func onTapLabel() {
  print("A")
}
1 Like

Remove " from the selector. It's a type-safe mechanism that does not require strings anymore.

1 Like

Remove the quotes around the function. It should just be #selector(onTapLabel)

1 Like

Thanks :heart: Fixed

2 Likes