Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

4 Questions about Selectors in Swift

Tags:

swift

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:"));

VS

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?

like image 415
JayVDiyk Avatar asked Jan 28 '15 03:01

JayVDiyk


People also ask

How does Selector work in Swift?

Use Selectors to Arrange Calls to Objective-C Methods In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure, and you create them using the #selector expression.

How do you call a selector in Swift?

The solution to your problem is to pass the object that should run the selector method along with the selector to the initialisation of the ValueAnimator object. Also update the timerCallback() : @objc func timerCallback() { ... _ = target.

What is IOS selector?

A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled. A selector by itself doesn't do anything. It simply identifies a method.


3 Answers

@ad121's answer is great—just want to add a little context to #1:

The Selector type has been extended in Swift to be StringLiteralConvertible. Any time a Selector instance is expected, you can give a string literal instead and a Selector instance will be created for you. This means you can also create a Selector instance from a string literal manually:

let mySelector: Selector = "methodName:withParameter:"

Note that this doesn't mean a String can be used interchangeably with a Selector—this only works with string literals. The following will fail:

let methodName = "methodName:withParameter:"
let mySelector: Selector = methodName
// error: 'String' is not convertible to 'Selector'

In that case you'd need to actually call the Selector constructor yourself:

let methodName = "methodName:withParameter:"
let mySelector = Selector(methodName)
like image 125
Nate Cook Avatar answered Jan 01 '23 20:01

Nate Cook


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().

like image 22
ad121 Avatar answered Jan 01 '23 20:01

ad121


Answering your 3rd question: You can totally do this. Just set the target parameter to the type itself.

Say you have a class defined:

class SomeType {
    class func someMethod() {}
    func someMethod() {}
}

Now taking your example, this will call the instance method:

let something = SomeType()
let closeBarButtonItem = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Stop, target: something, action: "someMethod")

Change the target and the call will get forwarded to the type:

let closeBarButtonItem = UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Stop, target: SomeType.self, action: "someMethod")
like image 42
Dmitry Avatar answered Jan 01 '23 19:01

Dmitry