Swift selector 3 with arguments

I have searched for the select method many times in Swift 3, but I have a lot of confusion.

1) what's the difference between Selector

and #selector

?

2) if i write with Selector

, does the designated function mean it is not available?

3) How to pass a parameter using a method #selector

.

My code

let button = UIButton()     
button.addTarget(self, action: #selector(getData(_:true)), for: .touchUpInside)
button.addTarget(self, action: Selector(), for: .touchUpInside)

func getData(_ isShowing:Bool){

    }

      

Can you help me clear my confusion?

Thank you for your valuable time

+3


source to share


2 answers


Answers to your questions:



  • A selector is a type. (to indicate that this is a function type). Whereas #selector is a function call. #selector

    → will return the type Selector

    . #selector

    checks if any function exists with the name of this function
  • The first answer will clarify this.
  • You can send the value through the sender like this. Example:button.layer.setValue(forKey:"someKey")

+2


source


I believe it #selector

is just a language construct that creates an object of the type Selector

. You want to use #selector

because the compiler actually checks if the method exists somewhere where with, Selector("abc")

you just run the constructor and don't check it.



+1


source







All Articles