JayVDiyk JayVDiyk - 8 months ago 15
Swift Question

4 Questions about Selectors in Swift

<--- Note: Please help vote UP this question if it helps you

Sorry for these questions

I have 4 questions about Selector in swift.

FIRST Question

I am wondering what is the proper way to use selector in swift

closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: Selector("closeBarButtonItemClicked:"));


closeBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Stop, target: self, action: "closeBarButtonItemClicked:");

Should we use Selector("methodName:") or "methodName:" right away?

Both way works but which one is the correct way?

SECOND Question

How do we call a function with a parameter in Swift? Let's say I want to call a function like this

func methodName(parameterOne : String, parameterTwo: String)

THIRD Question

How do we call a type method using Selector in swift? is it even possible at all?

class SomeClass {
class func someTypeMethod() {
// type method implementation goes here

FOURTH Question

What is the purpose of that colon behind the function name in Selector?


Question 1: I don't think there is really a correct way. I personally prefer the second way, but both work so I don't think it really matters.

Question 2: I just reread you question. I think you mean how to call that in a selector. The selector for that I believe would be "methodName:parameterTwo:" but I am not positive, as the selector with two parameters probably should have an external parameter name to place in the selector where parameterTwo is in my answer.

Old question 2 answer (prior to edit): You would call that function as methodName(variable1, parameterTwo: variable2). If you wanted to make them say the parameter name in the call you could make the header methodName(calledVarName parameterOne: String, calledVarName2 parameterTwo: String). This would be called as methodName(calledVarName: variable1, calledVarName2: variable2). You could also define the header as methodName(#parameterOne: String, #parameterTwo: String). This would be called as methodName(parameterOne: variable1, parameterTwo: variable2). Read more here: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

Question 3: I can't say for sure, but I dont think there is a way to make a selector for this. If there is I assume it would be "someTypeMethod"

Old question 3 answer (prior to edit): You can call this method by SomeClass.someTypeMethod().

Question 4: The colon signifies that the function header has a parameter. So "function1:" corresponds to func function1(someParameterName: AnyObjectHere) while "function1" corresponds to func function1().