user1673099
user1673099

Reputation: 3289

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

Upvotes: 3

Views: 2937

Answers (2)

Sivajee Battina
Sivajee Battina

Reputation: 4174

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")

Upvotes: 3

Alistra
Alistra

Reputation: 5195

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.

Upvotes: 1

Related Questions