So I was doing some reading, and I wanted to know what the following meant. Let's say for instance in Swift, you have a method like recordResponse(_:) what does the parameter mean?

I'm working with MVCs and was doing an exercise in a book that had that preset parameter. In the model, I'm supposed to use that method to record a passed answer of "true" or "false" (obviously a string not a boolean), to indicate that a current response was given from a method "currentQuestion".

The exercise in the online book also states that the same method recordResponse(_:) should check if the answer given from the question currentQuestion() method is correct or not.

4

2 Answers

It's a way of stating the name of the function. The function might be declared like this:

func recordResponse(s:String) { // ... } 

Now, by default, the s is an internal parameter name only; the external parameter name is suppressed. So we need a notation that describes the function, recordResponse, as taking one parameter with no external name. That notation is recordResponse(_:). The colon follows each parameter name — here there is only one parameter, and has no external name, which is what the underscore indicates.

In the usage you are asking about, this notation is just that: a notation. It's a convention for giving the name of the function in a complete way, when humans talk to humans (as in a tutorial). But in Swift 2.2, this notation will become very important, because it will be part of the language — it's how you'll form a function reference. That is, it will be Swift's own name for the function.

1

I am giving a example for this

func test() {

let strOne : String = "hello" let _ : String = "Linuxn00b" print(strOne) 

}

in let strTwo line xcode produce a warnig that Having an "_" the "named parameter" will be explicitly unused.Happy coding .

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy