Reputation: 1828
Can anyone explain this piece of Swift syntax to me please:
func someFunc(label label: String, image: UIImage){}
I am wondering what the first label
parameter means? Why doesn't it have a type? Why isn't there a comma separator?
Upvotes: 1
Views: 302
Reputation: 438212
The others have answered the question about the syntax of external names for the parameters in Swift 2, so I won't repeat that here.
But I'd encourage you consider the method name when naming the parameters. Let's say you had some method inside your cell class where you were updating the label and image. Then the method might be defined as
update(label label: String, image: UIImage) { ... }
Then you'd call it as
update(label: "foo", image: barImage)
But in Swift 2, it's generally advised to incorporate the identifying information in the method name itself, e.g.
updateLabel(label: String, image: UIImage) { ... }
and then you'd call this as
updateLabel("foo", image: barImage)
The key observation in naming methods is that the last noun in the method name frequently references the first parameter (and thus why we generally don't supply an external parameter name for the first parameter). But if that last noun in the method name doesn't reference what the first parameter is, then you might supply an external name for that parameter.
It's worth noting that the above discussion applies just to Swift versions prior to Swift 3, where they defined the naming of parameters (e.g. where the first parameter's name is incorporated into the method name, and there is generally no label on the first parameter) to mirror the Objective-C API prevalent in Cocoa at the time. In Swift 3, this convention has changed.
In Swift 3, they explicitly suggest that the method name not include the name of the first parameter, so you'd define the above to be:
func update(label: String, image: UIImage) { ... }
But furthermore, when you call this Swift 3 function, you would supply the first parameter's label (even though an explicit external label was not included in the original declaration):
update(label: "Foo", image: someImage)
If you needed the Swift 2 pattern to suppress the first (or any parameter), in Swift 3 you can do that with _
:
updateLabel(_ label: "Foo", image: someImage)
But, while you can suppress a parameter (with _
in Swift 3), it's not generally advised to do so unless there is some other compelling reason to do so.
Upvotes: 3
Reputation: 4641
When you give two names to the variable, the first one is used by callers of the function, and the second is used inside your function.
See this documentation, scroll down to Specifying External Parameter Names. Here's an example from that section.
Here’s a version of the sayHello(_:) function that takes the names of two people and returns a greeting for both of them:
func sayHello(to person: String, and anotherPerson: String) -> String {
return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(to: "Bill", and: "Ted"))
// prints "Hello Bill and Ted!"
By specifying external parameter names for both parameters, both the first and second arguments to the sayHello(to:and:) function must be labeled when you call it.
The use of external parameter names can allow a function to be called in an expressive, sentence-like manner, while still providing a function body that is readable and clear in intent.
Upvotes: 4
Reputation: 2458
When using a function in swift every argument has two names, an external and an internal. While all the arguments except the one can have both internal and external the same name on the first argument in order to have internal and external name you will need to specify it twice like:
func someFunc(label label: String)
The external name is always the left one and the right is the local. When you have more than one arguments then if you want to have different internal with external name you can specify it as follows:
func someFunc(label label: String,externalName image: UIImage){}
The internal argument name is used inside the function and the external name will be right after you call the function.This will be the result when you call someFunc
:
someFunc(label: ..., externalName: )
You can completely remove the name from the second argument by adding _
instead of it's name. I suggest you have a good look here in order to get a good grasp of the functions in swift. Functions in Swift
Upvotes: 2