Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 3 selector with arguments

I searched a lot for selector method in Swift 3, but I have lots of confusion for it.

1) what is difference between Selector & #selector?

2) if I write with Selector, the function is outlined means not available?

3) How to pass a parameter with #selector method.

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 to clear my confusion?

Thank you for your valuable time

like image 475
user1673099 Avatar asked Jun 20 '26 05:06

user1673099


2 Answers

Answers to your questions:

  1. Selector is a type. (to indicate that it's a function type). Whereas #selector is to call a function. #selector --> will return Selector type. #selector checks if there is any function exist with that function name
  2. First answer will clarify this
  3. You can send value through sender like this. Example: button.layer.setValue(forKey:"someKey")
like image 141
Sivajee Battina Avatar answered Jun 21 '26 17:06

Sivajee Battina


I believe #selector is just a language construction that creates an object of type Selector. You want to use #selector as the compiler actually checks if the method exists anywhere, where with Selector("abc") you just run the constructor and it's not validated.

like image 30
Alistra Avatar answered Jun 21 '26 19:06

Alistra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!