What is a selector in SKAction: execute (_: onTarget :)

The docs say:

Declaration :

class func perform(_ selector: Selector, onTarget target: Any) -> SKAction

selector

Method selector to call.

I'm not sure what a method selector is. Hence the question.

It seems like this will be the name of the method / function, but it creates (in me) uncertainty because it was never described as such, so I seem to think it could be something else, something deeper, maybe.

I assume that perform(_:onTarget) is a way of partial flexibility in passing part of the code to an object that decided to perform some action at runtime. But I'm not quite sure that I have a goal. This is the context in which I think about it.

Not only is my question different from the related β€œsimilar” question in terms of context, it is also another and much more specific question: WHAT is a selector in this particular function.

+3
source share
1 answer

The selector is the name of the function, and the target is the object for which the function is executed. You create a selector using the syntax: #selector(<function name>) , for example:

 class MyClass { func createAction() { let action = SKAction.perform(#selector(MyClass.myActionFunction), onTarget: self) // ... } @objc func myActionFunction() { // do stuff } } 

To create a selector for a function that takes arguments, use the syntax:

 #selector(MyClass.myActionFunction(arg1:arg2:)) 

You can also do the same using a block instead of a selector:

 let action = SKAction.run { [weak self] in self?.myActionFunction() } 
+2
source

Source: https://habr.com/ru/post/1012072/


All Articles