3

The docs say:

Declaration:

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

selector

The selector of the method to call.

I am uncertain what a selector of a method is. Hence the question.

It seems like it would be the name of the method/function, but creates (in me) uncertainty because it's never described as being this, so I kind of think it might be something else, something more profound, perhaps.

I'm presupposing perform(_:onTarget) is a way for a part of code to be flexibly telling an object decided at runtime what action to perform. But am not entirely sure that I have its purpose right. That's the context within which I'm thinking about this.

Not only is my question different from the linked "similar" question in terms of context, it's also a different, and much more specific question: WHAT is a selector in this particular function.

Community
  • 1
  • 1
Confused
  • 6,048
  • 6
  • 34
  • 75
  • 2
    See: http://stackoverflow.com/questions/24007650/selector-in-swift – dalton_c Nov 01 '16 at 05:42
  • @daltonclaybrook ok. I'm dense. I've read all those answers, but still don't have a clue what I'm supposed to insert as a selector so as to use SKAction: perform(_:onTarget:). – Confused Nov 01 '16 at 07:55

1 Answers1

2

A selector is the name of a function, and a target is an object on which to perform the function. You construct 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 accomplish this same thing using a block instead of a selector:

let action = SKAction.run { [weak self] in
    self?.myActionFunction()
}
dalton_c
  • 6,876
  • 1
  • 33
  • 42
  • why do you have the `@objc` in there? – Confused Nov 01 '16 at 14:28
  • 1
    Selectors are a construct of Objective-C, which uses message sends to call functions. Since we are working in Swift, we have to expose the function to the Objective-C runtime in order to use it as a selector. Using `@objc` is one way to do this. The other way is to make `MyClass` inherit from `NSObject` – dalton_c Nov 01 '16 at 14:32
  • I'm presupposing `perform(_:onTarget)` is a way for a part of code to be flexibly telling an object decided at runtime what action to perform. But am not entirely sure that I have its purpose right. That's the context within which I'm thinking about this. Perhaps I have entirely the wrong idea about what `perform(_:onTarget)` is all about ? – Confused Nov 01 '16 at 14:35
  • From what you're saying about Objective-C, it might be that perform(_:onTarget) might be a facility to help bridge code bases that are in both Swift and Objective-C, and therefore I have completely the wrong idea about it. – Confused Nov 01 '16 at 14:37
  • 1
    Selectors have existed in Objective-C longer than blocks (closures) have. Before blocks, selectors were extremely useful because objects (SKAction in this case) didn't need to know anything about the target in order to call a function on it. Today, and especially in Swift, it's more common to pass a block to an object, which it then retains and executes asynchronously. It accomplishes the same purpose. – dalton_c Nov 01 '16 at 14:40
  • ok... that might be perfectly comprehensible to someone that knows what you're talking about, but not to me, at all. – Confused Nov 01 '16 at 14:43
  • Sorry if my responses are confusing. I've edited my answer to show an analogous use of blocks instead of selectors. – dalton_c Nov 01 '16 at 14:45
  • It's perfectly fine. I'm accustomed to programmers talking about things as if everyone knows what they're talking about. The problem with that is that there's information asymmetry inherent in the need to ask a question as naive and ignorant as mine. I have (at the time of asking the question) ZERO understanding, knowledge or insight into Selectors, their purpose, reasons for being, usage criteria, syntax, etc. NOTHING. Perhaps less than nothing. And it would be fair to say I'm struggling to come to terms with closures being unnamed functions, passable, and of a type in their own right, too. – Confused Nov 01 '16 at 14:49
  • In your example, could myActionFunction be ANY type of function, not necessarily an SKAction? – Confused Nov 01 '16 at 14:51
  • 1
    Not sure I understand the question. It's not specifically tied to `SKAction` right now. Technically, `myActionFunction` has a type: `() -> ()`. i.e. it takes no arguments and has no return value. – dalton_c Nov 01 '16 at 14:56
  • as far as I understand it, `SKaction.run(_here_)` is expecting an SKAction. From your demo of a block, it looks like that's not the case, that any function can be put in there. Is that right? – Confused Nov 01 '16 at 15:14
  • 1
    Ah, I understand your question now. `SKAction` has two separate functions called `run`. One takes an SKAction, and the other takes a block. Even though they have the same name, the functions can be distinct because they take arguments of different types. This is called function "overloading." – dalton_c Nov 01 '16 at 15:20
  • argh... some lights flickered on in a very dull attic. Time to clean out some cobwebs. So the passed argument type determines which `run` is done. I think I get it, now. And yes, this does seem to suggest there's little to no need to think about Selectors in Swift. Which is a level of relief akin to finding out a rash is just a rash. – Confused Nov 01 '16 at 15:26
  • I typically try to avoid anything Objective-C while working in Swift. It just simplifies things. – dalton_c Nov 01 '16 at 15:30