|
I'm having a minor comprehension crisis with the following example from https://www.hackingwithswift.com/quick-start/beginners/how-to-accept-functions-as-parameters
In the example, we're only passing a single parameter into makeArray, but when I try to replicate this in my Playground I get an error, since makeArray requires two parameters, however I have no idea what to pass in as the second parameter. e.g. let rolls = makeArray(size: 50, ?something?) Have I missed the point? Do I need to define generator somewhere else? What does generator actually do? |
|
You don't indicate exactly what error you are getting in the playground, so it's a little difficult to assess what's wrong with your code, but in the example you give you are actually passing two parameters to The signature of
meaning that the second parameter is a function that takes no input and returns an
But we can simplify by replacing the explicit function
Swift has a nifty feature called trailing closures, where if the final parameter is a closure* you can write the closure after the closing parentheses "even though the trailing closure is still an argument to the function", that allow us to clean up that call site:
So you are still passing two parameters to Admittedly, it takes some getting used to when you first pick up Swift, but it really makes your code easier to read and nicer looking. *And, since last year, you can even have multiple trailing closures. So a something like this, that takes two function params:
can be written as:
You'll notice there are no parentheses but you are still calling the |
|
Thanks for the reply, @roosterboy. I'll try to explain my issue, but I'm already feeling out of my depth since the beginning of Day 9 of 100, so please bear with me. The examples I posted are copied verbatim from the website, so I'm just trying to understand how they (don't) work. I'm getting the error "Missing argument for parameter 'using in call'" on the line:
So, I did attempt to put the additional parameter into it, like:
Playground then gives me the error "Cannot find 'using' in scope" I'm more of a "follow the examples" kind of learner, so I get thrown off easily when I copy something verbatim and can't get it to work. It's likely that I don't understand the concepts well enough, but after repeating Day 9 three times I'm wondering whether I'm just not up to the task. |
|
Since
|
|
@snaketide thank you for this question, my head was hurting too.... @roosterboy thank you for your explanation seeing the following together on screen helped me see what was happening
at the moment I find this easier to read before it's converted to a trailing closure, the closed parentheses threw me.... I'm getting there though... but I'm not looking forward to checkpoint 5, I think it's going to hurt, lol |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.