|
Hello folks, and thank you so much Paul for this resource. I feel like I'm learning a ton π But... first day learning about closures has hit my brain hard! For the challenge/checkpoint, I've made a start, got the right structure (from the earlier examples), and have started to make some progress but immediately got stuck. I've changed the first closure to return an array of Int. Yay. But for the life of me I can't get it to accept an array as input. Am I not meant to do that? It's been a while since I've used git but hopefully this link will let you see my code: https://github.com/davenewt/100Days-SwiftUI-Day9 Thanks in advance for any guidance! π |
|
Where are your 'luckyNumbers' created - I see them being used, but I don't see them defined. (i.e. let luckyNumbers = [1,2,3,4,5]). BTW, Swift starts arrays at element 0. |
|
Welcome to Hacking With Swift!Your question might be very useful to a new programmer one, maybe three years from now. Please post your smaller code snips directly in your forum message! Reading a SignatureIt's early days in your Swift journey. So please make lots of errors, revise your code and continue learning. One important lesson you need to practice over and over is How to Read Function Signatures. For example, this is your function's signature:
Can you read this signature? Does this make sense to you? You need a skill to read this signature OUT LOUD like it's a sentence. Keep reading it until it makes sense to you. Here's what your signature says:
Your function tells a programmer that it needs three inputs, named
Yikes! I don't think this is what you want to do! Additional ReadingHere's a short response to another new programmer who was learning to read function signatures. See -> Signatures! Read All About It Keep Coding!More feedback! |
|
Forum regular @roosterBoy posted a great article on how to post code bits to this forum. See -> Posting Code to the Forum Go on! Give it a try! Keep Coding! |
|
@cacruden β thanks for the reply... the array is being defined at the top of the code. @Obelix β thanks also! Ok, here's my code pasted into the forum...
I understand what the I've only started with the I thought I first needed to get Am I on the right lines so far? Hopefully also this code shows up OK, I don't see a |
|
Whilst typing your forum message, you'll see a small eyeball ποΈ above the text box where you type. Click once for a preview! Click again to exit preview mode and return to editing mode. ALSO, you can always return to a post that YOU WROTE and edit it over and over again. Keep Coding |
|
@daveAsaur: You might be going down the wrong path. Here's a code snip for you to review. But inside this code snip is a BIG HINT on how you should proceed. Please keep making mistakes. It's a great way to challenge yourself and learn.
Note! In the code above, Keep Coding |
|
@Obelix thank you so much for your patience. I'm trying hard to wrap my brain around this, I really am.
I've seen people commenting on the original video saying "I was able to do this with one line of code!" so why am I writing so many lines of code and seemingly nesting functions (which can't be a good idea) and still not getting it?! I feel SOOOO stupid π’ |
|
OK I've gone back to Paul's videos again ! This part helped https://youtu.be/HnIID4W3a_o?t=420 And this code works...
I will admit that I have the predictive code completion enabled (running xcode beta) so I should probably turn that off, because it auto-completed the (and in Paul's video he covers 'map' which I'd forgotten existed, here: https://youtu.be/HnIID4W3a_o?t=484) @Obelix β is there a reason you were suggesting
rather than simply ? I feel like I might be missing a big chunk of what you were trying to teach me! Apologies if this is the case! |
|
And now I understand how THIS works...
|
|
rather than simply Great Question!The answer is Yes!
How can you tell the second example is a function? This is a skill you need to learn! Hover your mouse over Xcode will show you that one is an LEARN THIS TRICK. Keep Coding |
|
OK I understand that example 2 is a function which returns an
? But you also say it's "hardly ever used" so... why is it important to know? (or should I stop stressing about it and it'll become clear later in the course?) π Thank you so much for your help. |
|
I should have clarified. Using a You will write functions and assign them to variables. And you'll write flexible functions and use them in closures. You will hardly ever write a function to return an array of static integers. Keep CodingI added two more lessons.
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.