pulp pulp - 2 years ago 82
Swift Question

Can anyone explain Swift syntax?

Can anyone explain this piece of Swift syntax to me please:

func someFunc(label label: String, image: UIImage){}

I am wondering what the first
parameter means? Why doesn't it have a type? Why isn't there a comma separator?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download