Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform Selector With Object in swift 3

I am trying to perform selector with object in swift 3.0

I have a selector which have one parameter

func imageSelected(aImage : UIImage)

and I am calling it like

viewC.perform(Selector.init("imageSelected:"), with: image, afterDelay: 0.1)

But the app crashes with error that the selector is not defined.

like image 595
Zaheer Abbas Avatar asked Jan 31 '17 07:01

Zaheer Abbas


3 Answers

Here's something I always do when I encounter selectors in swift: Ignore the parameters, just use the name.

You used this:

imageSelected:

What is that : doing there? Delete it! Just use the name of the method!

Also, there is this great #selector syntactic sugar, please use that:

viewC.perform(#selector(imageSelected), with: image, afterDelay: 0.1)
like image 100
Sweeper Avatar answered Nov 09 '22 17:11

Sweeper


This is for swift 4.0

perform(#selector(yourMethodHere), with: nil, afterDelay: 1)

Add @objc flag before your function

@objc public func yourMethodHere(){
     //your code here
}
like image 33
sonali Avatar answered Nov 09 '22 16:11

sonali


It started working well as, I modified the selector being called

from

func imageSelected(aImage : UIImage)

to this

func imageSelected(_ aImage : UIImage)
like image 34
Zaheer Abbas Avatar answered Nov 09 '22 16:11

Zaheer Abbas