What Is _: in Swift Telling Me

What is _: in Swift telling me?

The _ is used to define that the parameter is not named

If you have multiple _ it states that you do not need to name the parameters in your function call

func myFunc(name:String, _ age:String){
}

myFunc(“Milo", "I'm a really old wizard")

If you do not use the underscore you would use

myFunc(“Milo”, age: "I'm a really old wizard")

The _ is not necessary for function calls. It is just used to indicate that something does not need to have a name.

In regards to how you would refer to your function, You would not have to pass any name for the function call.

But since you also don’t define a parameter type this seems to me like an invalid example (it at least doesn’t work in Xcode 7 with Swift 2.0)

Edit:

Since Swift 3.0

myFunc(name: “Milo”, age: "I'm a really old wizard")

Should be used

What do _:_: and similar combinations of the colon and underscore mean in Swift?

The underscore indicates that there is no external parameter name for the function. Apple's Swift Documentation talks about this concept in terms of when you're writing your own functions.

Take the case where you write this function (from the documentation):

func sayHello(to person: String, and anotherPerson: String) -> String { ... }

If you were to use the function you would write the following:

sayHello(to: "Bill", and: "Ted")

The signature for this would be sayHello(to:and:).
However, what if we wanted to use the function as sayHello("Bill", "Ted")? How would we indicate that we didn't want an external parameter name? Well that's where the underscore comes in. We could rewrite the function as:

func sayHello(person: String, _ anotherPerson: String) -> String { ... }

Note the first parameter doesn't need the _, but subsequent ones will. The first is inferred to have no parameter name. This makes the method signature for this call sayHello(_:_:) because you as the caller don't have a named parameter.

Update Swift 3.0:

Swift 3.0 treats all parameters equally. The first parameter now requires an underscore to indicate the absense of an external parameter name. In the above example of having sayHello("Bill", "Ted") at the call site, your corresponding function or method declaration would have to be

func sayHello(_ person: String, _ anotherPerson: String) -> String { ... }

Note the addition of the underscore before the internal parameter name 'person'.

What does this underscore mean in Swift?

An underscore denotes the fact that you need to set a variable, but you do not plan on using the variable in the future. Instead of giving it an elaborate name, an underscore will be more simple and less clutter, especially in the case that the variable name doesn't matter. (since you do not use it again.)

What does the underscore in a function declaration do?

The underscore means that parameter name should be omitted when calling the function.

So this function:

func webViewDidFinishLoad(_ playerWebView: UIWebView) { /* ... */ }

should be called as:

webViewDidFinishLoad(aWebView)

and this one:

func webViewDidFinishLoad(playerWebView: UIWebView) { /* ... */ }

as:

webViewDidFinishLoad(playerWebView: aWebView)

In Swift, those are seen as two different functions and that's why you don't see the function being called when you change its signature.

More information can be found here, under "Function Argument Labels and Parameter Names":

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID166

What's the _ underscore representative of in Swift References?

Both answers were correct but I want to clarify a little bit more.

_ is used to modify external parameter name behavior for methods.

In Local and External Parameter Names for Methods section of the documentation, it says:

Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default.

On the other hand, functions by default don't have external parameter names.

For example, we have this foo() method defined in class Bar:

class Bar{
func foo(s1: String, s2: String) -> String {
return s1 + s2;
}
}

When you call foo(), it is called like bar.foo("Hello", s2: "World").

But, you can override this behavior by using _ in front of s2 where it's declared.

func foo(s1: String, _ s2: String) -> String{
return s1 + s2;
}

Then, when you call foo, it could be simply called like bar.foo("Hello", "World") without the name of the second parameter.

Back to your case, runAction is a method because it's associated with type SKNode, obviously. Thus, putting a _ before parameter action allows you to call runAction without an external name.

Update for Swift 2.0

Function and method now work the same way in terms of local and external argument name declaration.

Functions are now called by using external parameter name by default, starting at 2nd parameter. This rule only applies to pure Swift code.

So, by providing an _ in front of a function, the caller won't have to specify external parameter name, just like what you would do for a method.

What is the purpose of under score in when declaring a IBAction func?

This place is declared for the label which means that once you call the function it won't appear to you for example:

   func sum (_ number1: Int, _ number2: Int) {
print(number1 + number2)
}

once you call the function you won't need to mention number1 or number2 but you will only write the number directly :

sum(1, 2)

To be clear it's the same as using the function below:

func summation(myFirstNumber number1: Int, mySecondNumber number2: Int) {
print (number1 + number2)
}

but here instead of using _ I've used a label so when I called the function, I will use these labels :

summation(myFirstNumber: 1, mySecondNumber: 2)

So it's clear now that _ is instead of writing a label.

For more information check: Function Argument Labels and Parameter Names from here

CoreML2 keeps telling me everything is the same thing

If you have model trained with 2 items only (in your case Apple and Banana) you can't expect that your ML model recognize something else than these 2 items.
Code that you wrote always returns item which will have the biggest confidence.


Anyway, if you have more items, you can do something like: if no item matches with testing image for at least x%, do this

guard let topResult = classifications.first else { return }

if topResult.confidence > 0.75 {
print(topResult.identifier)
} else {
print("Match is less than 75%")
}


Related Topics



Leave a reply



Submit