3

I want to add a space between parameter names of a swift function. I want to achieve something like in the prepare for segue function provided by swift

func prepare(for segue: UIStoryboardSegue, sender: Any?)

That "for segue" parameter name that is used on the function call when you implement it.

Eg. I want to create a function func saveReceipt(for customer:....) then I want to call the function saveReceipt(for customer:....)

  • 3
    "*That "for segue" parameter name that is used on the function call when you implement it*" – no, it's called as `prepare(for: ...)`. `for` is the argument label, which is used at the call site, `segue` is the parameter name, which is used internally inside the method. – Hamish Jan 26 '17 at 10:40

2 Answers2

2

That is not how parameters work sadly.

Take the example of the prepareForSegue func. It looks like this as a func:

override prepare(for segue: ...) {

However, if you were to call it, it would look like this:

prepare(for: UIStoryboardSegue, ...)

You can only ever have one parameter name for when you call a func.

Edit

To look at your example, you could have a func like this:

func saveReceipt(for customer: ...) {

And then call it like this:

saveReceipt(for: MyCustomerClass, ...) 
Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
0

In your example for is the argument label whereas segue is the parameter name. This "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." (see https://docs.swift.org/swift-book/LanguageGuide/Functions.html)

That is, you use the argument label for the caller and the parameter name inside the function body.

In your second example your call saveReceipt using saveReceipt(for: myCustomer) (myCustomer is the variable holding your customer) and use customer as the parameter name inside the body.

Björn Kahlert
  • 109
  • 1
  • 5