LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Solving Checkpoint 9

Forums > 100 Days of SwiftUI

Hey everyone. I apologize as it's been a while and I just got back to learning swift. The thing is, I'm having trouble understanding and retaining whatever I'm learning. Is this normal or am I really just stupid? Anyways, here's my solution for checkpoint 9 so far

// Write a function that accepts an optional array of integers, and returns one of those integers randomly
// If the array is missing or empty, return a new random number in the range 1 through 100
// Write your function in a single line of code

func integerRandomizer() -> [Int]? {
    return if let intArray = optionalValue ?? defaultValue?.randomElement()
}

I'm not entirely sure if this is on the right track but I can't seem to wrap my head around how to solve this problem. Also, it's not also one line since I have a func integerRandomizer. I can't remove the function either because I need to return something. Can anyone guide me with this? I'm also having trouble with if I should also initialize intArray but then if I were to initialize intArray, I'd be adding another line of code. Apologies for the stupid question but I'm just trying to get back into the groove after weeks of hiatus. Any guidance is sincerely appreciated.

3      

A few things:

  1. You don't pass in any parameter (your function should accept "an optional array of integers") yet you somehow use an optionalValue variable in the body of your function. Where does optionalValue come from? Where does defaultValue come from, for that matter?

  2. You cannot do return if let ...

  3. You have an if statement where you check a condition but then you don't do anything.

  4. Keep in mind that Swift will return the value of a single line function without the need for the return keyword.

  5. You return an array of Integers ([Int]) but in the body of your function you are returning a single value from an array.

  6. You return an optional array of Integers ([Int]?) but you are using if let and nil coalescing in your function to ensure you don't return an optional value.

  7. You completely leave out the part about "If the array is missing or empty, return a new random number in the range 1 through 100"

Think about these things and take another stab at writing the function.

3      

If I were you. I would put plenty comments in your code. You wont remember everything! It is impossible! If your comments get too big you can comment to where you keep a document that explains your code exactly how it works. Lots of pictures video etc. You can comment weblinks where you found relevant information, but thats not quite reliable since websites are like umbrellas. They go up and they go down. Best to keep on file. If your not understanding the language syntax then google different examples. Although this site is a treasure trove of information sometimes things dont click so find some other videos articles on what your stuck on. Sometimes when you hear it from someone else it clicks, then when it does click come back to here and youre like wow I know exactly what he means now. When you finally click make sure you comment /document how you understand it. Lastly dont stress about it. Itll take time dont be disheartened if you dont understand it yet. Youll get there! Even the most clever and experienced have looked at things and went...What the True or False?? WTF? for short lol.

Good luck!

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.