“Unrecognized selector sent to instance” in swift

后端 未结 10 466
刺人心
刺人心 2020-12-10 10:36

I have no idea what I am doing wrong. I am also quite new to programming so I am not very good at debugging. This was a test app so that I can see how swift ties in with app

相关标签:
10条回答
  • 2020-12-10 11:08

    The syntax should be

    @IBAction func buttonAction(_ sender: UIButton) {
         print('tapped')
     }
    

    Either the syntax is the issue or

    The Button on the storyboard has several registered actions to it. To find out just right click button and you should be able to see how many connection there are

    if the touch up inside has more than one connection delete one. This is because if you have multiple connections and you click on the control the compiler will be confused as to which one it should implement

    0 讨论(0)
  • 2020-12-10 11:10

    If you have a colon in the selector name (buttonAction:) you have to have the sender as a n argument for the buttonAction function, if you don't have any colon in the name the sender will not be sent.

    0 讨论(0)
  • 2020-12-10 11:10

    There are two other causes I ran into which can lead to this type of error:

    1) When the first argument to addTarget does not inherit from NSObject. For example this will not work:

    class MyClass {
        init(button: UIButton) {
            button.addTarget(
                self,
                action: Selector("onClick:"),
                forControlEvents: UIControlEvents.TouchUpInside)
        }
    
        func onClick(sender: AnyObject?) {
            //Gotcha!
        }
    }
    

    }

    To fix just change to:

    class MyClass: NSObject
    ...
    

    2) When you don't hold onto an explicit reference for the instance with the selector. For example if using the same above code:

    func crashMe() {
        var delegate = MyClass(button)
    }
    

    Swift seems to garbage collect "delegate" even though it's listening to button, hence a crash will occur. To fix, change to something like this:

    func crashMe() {
        self.delegate = MyClass(button)
    }
    

    I also ran into BOTH the "private" and "crashMe" versus "crashMe:" issues while developing my app... Hence taking the time to write this post to sum up the traps you'll run into. :)

    0 讨论(0)
  • 2020-12-10 11:14

    In your viewDidLoad, notice my Selector is calling my func with the colon.

    override func viewDidLoad() {
            super.viewDidLoad()
            // init gesture recognizer swipe right vars
            var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("gestureFunc:"))
            rightSwipe.direction = UISwipeGestureRecognizerDirection.Right
            view.addGestureRecognizer(rightSwipe) 
        }
    

    Then make sure that the Selector i.e. func is ready to receive it's swipe:

            // selector from UISwipeGestureRecognizer
            func nextWeapon(gesture: UISwipeGestureRecognizer) {
            println("swipe right")
            }
    
    0 讨论(0)
  • 2020-12-10 11:23

    when using performSelector() methods your method (matching the selector) should be marked as @objc

    {  
        //...
        performSelector(Selector("myMethod"), withObject: nil, afterDelay: 0.3)
        //...
    
        // or in swift 2.2
        performSelector(#selector(ClassName.myMethod), withObject: nil, afterDelay: 0.3)
        //
    
        // or in swift 3
        perform(#selector(ClassName.myMethod), with: nil, afterDelay: 0.3)
        //
    
    }
    
    @objc private func myMethod() { }
    
    0 讨论(0)
  • 2020-12-10 11:24

    Got this working by adding an "_ " before the argument.. refer to snippet below

    func buttonAction(sender:UIButton)  // this was throwing the error "unrecognized selector sent to instance"
    {
        println("tapped button")
    }
    
    func buttonAction(_ sender:UIButton)  // added an "_ " before sender
    {
        println("tapped button")
    }
    
    0 讨论(0)
提交回复
热议问题