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:....)

1
  • 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. Commented Jan 26, 2017 at 10:40

2 Answers 2

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, ...) 
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, ok, I got it, always I thought that "for segue" is the parameter name used on the call
@VicentiuPetreaca Yeah no, the "segue" label is strictly internal. :)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.