|
Going through Day 9 and am absolutely stumped here on how this closure should work from this optional guide: https://www.hackingwithswift.com/quick-start/understanding-swift/when-would-closures-with-parameters-be-used-as-parameters
I have tried everything imaginable to use this buildCar function and pass in a closure, been banging my head on it for over an hour now and have no idea how to actually use it. This is the closest I've gotten, which still doesn't print anything at all, doesn't error but does give a warning of "Constant 'jeep' inferred to have type '()', which may be unexpected"
|
|
I don't think that Paul is actually expecting you to write the code to build the car here. I think he was just putting in that comment to say "The code for building the car would go here, but we're not going to get into all that right now. Right now, I'm just trying to show you that a closure with parameters can be passed into a function as a parameter." But, I find this example to be very confusing. It basically tries to say that we need an engine to put into a car, and the function of changing speed is representing the whole physical engine that we are putting into our car for some reason. I don't really understand that myself. I think it would have made more sense to give an example like this...
You see that we can pass either our You might notice that you could have set the car to 10kph last, but then use this
And it would print |
|
@xan is driving on the wrong side of the road:
At times like this, pull over to the shoulder, take a good look at the map, and ask the age old question, "What is the compiler telling me?" See -> What type is this? Using the technique described above, hold down the option key and hover over the variable For example:
When you hover over your variable What type is jeep ?You are setting a constant by using The compiler is correctly telling you that How to fixIndeed, sometime you want code to just run and you don't care about the return type. In the CS193p courses, Paul Hegarty calls these variables Don't Cares. So, if you don't care what type In Swift, please use the underscore character to tell XCode that you don't care.
How to fix, part II@FlyOstrich shows another way to fix, but this only works in Playgrounds. Instead of assigning to a constant, just run the function!
|
|
Awesome, appreciate both the responses, helped me learn a lot. Still only on day 10 now but loving it, coming from PHP-land. It turns out this page might not even be linked anywhere normally. When on Day 9 it's not any of the optional reading, but if you start at the top optional article and keep hitting the next article link in the top you end up there. I'm not sure why there's links at the top left and top right for optional reading that don't seem to follow the order of the lessons, but that's how I got lost trying to figure out how to do something before reading about how to do it haha. Thanks again, and have a great next few days! |
|
I must be doing something wrong in this post because it's not formatting the code properly. I put three double quotes around the code. Is that not the right way? But the original question remains unanswered: How do you call buildCar?
buildCar is a function that takes a String and a closure of type (Int) -> Void and the function returns nothing. We've already created a closure called changeSpeed of type (Int) -> Void
My thinking was to pass in changeSpeed to the buildCar function for the engine parameter
Shouldn't that work? In playgrounds, it does not print anything. In fact, this prints nothing as well:
How should buildCar be called? A couple of asides:
Thanks, Rob |
|
Not double quotes, backticks: Or just hit the If you hit that button while on a line that already has text, you'll get the markdown for inline code: |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.