Perform Selector With Object in swift 3

烂漫一生 提交于 2020-01-13 07:36:06

问题


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.


回答1:


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)



回答2:


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
}



回答3:


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

from

func imageSelected(aImage : UIImage)

to this

func imageSelected(_ aImage : UIImage)


来源:https://stackoverflow.com/questions/41951473/perform-selector-with-object-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!