WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Cpopp9 - Day 9 question

Forums > 100 Days of SwiftUI

Day 0

Hey everyone!

I'll be using this thread to post updates as to my progress as I complete each day, similar to other posts I've seen on the forum.

I'm coming into this 100 challenge with zero programming knowlege outside of poking around config files in PC games as a teenager (Battlefield 1942!) and am excited to see how well I can adapt to this new field - or if it's even for me at all.

Looking forward to learning with all of you!

   

Day 1 Completed

I accidently completed the 100 days of Swift first before realizing I wasn't in the SwiftUI course - whoops!

I found the topic of variables, strings, ints, etc all to be simple enough to grasp, and had some extra time to play around with them in playgrounds to get used to using them.

I'm tempted to do more than one day at a time, but I'll resist the urge for now.

   

Day 2 Completed

I was able to complete the first checkpoint successfully the first time around, so I'm feeling confident so far.

After completeing the checkpoint, I began playing around with the code some more and wanted to see if I could change the value of fahrenheit by adjusting the value for celsius.

import Cocoa

var celsius = 35.2
var fahrenheit = celsius * 9 / 5 + 32

celsius = 0
print(celsius)

print(fahrenheit)

I get the correct fahrenheit value the first time, but after changing celsius to 0, I couldn't get fahrenheit to recalculate even after the value for celsius had changed.

My guess is that the print command has swift print the last stored value for fahrenheit instead of recalculating the value. I can get it to recaulculate by adding:

fahrenheit = celsius * 9 / 5 + 32

but is there an easier way?

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, 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.

Save 50% on all our books and bundles!

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

Hi Cory,

A couple of points. I assume you are doing this in a Playground this run code from top to bottom so if you change the celsius then you have to do the recalculation again.

As you are just at the beginning of your journey. I will try not confuse you to much but yes there is a number of ways this can be done

var celsius = 35.2
//var fahrenheit = celsius * 9 / 5 + 32 <- Not needed

func convertToFahrenheit(from celsius: Double) -> Double {
    celsius * 9 / 5 + 32
}

celsius = 0
print(celsius)

print(convertToFahrenheit(from: celsius))

You will learn about Functions very soon. Hope you enjoy your 100 day journey.

PS If you have a question just add "Question" to the title of the thread then people will know that you have a question not just update you day count.

   

Day 3 Completed

Arrays, dictionaries, sets and enums

Although I know I could be moving through the material faster, I've been taking my time with the concepts by altering the samples to explore how each type of data grouping works, and lots of trial and error to make sure I'm getting the output I'm expecting every time I try something new.

   

Cory contemplates rushing:

Although I know I could be moving through the material faster...

You had the skills, equipment, and drive to film a Fishtown video in a day. Yet you spent six days capturing b-roll material in Fishtown and countless more editing! You can take your time with the 100 Days program. The story in your code will take time to form. There's a great team of graduates here ready to help. Let us know what is confounding and we'll add different lighting to the subject and help bring the image into focus.

   

Day 4 completed

I was able to complete the day 4 challenge but had to look up how to correctly establish a new Set. The code I was using to create the set was:

var uniqueNames = Set([names])

But I was able to correct that line using the hint provided underneath the checkpoint, which was:

var uniqueNames = Set(names)

   

Day 5 complete

Conditions are fun, but I get the feeling it'll take me a while to figure out exactly when and where to use each.

   

Day 6 Completed

Fizz Buzz! I was very proud of myself for being able to solve this checkpoint without needing hints and with relatively few hiccups.

Thankfully I had experimented with loops in my own playground prior to this test, so I was already aware of some of the mistakes I was making like forgetting to create a condition after starting the loop.

   

Day 7 completed

I struggled with functions, and I know I don't have it down 100% so will likely need to return later to make sure I truly understand the ins and out of the concepts.

   

Day 8 Completed

Checkpoint 4 was a real struggle for me, and even with all the hints I wasn't about to come up with the correct answer. After reviewing the walkthrough for the challenge, I was able to see where I was going wrong, and the answer makes since to me, but I can tell I just need more practice.

   

Day 9 Completed

While learning about closures, I got hung up on how this example function works for sorting names by a custom order and was wondering if someone could help me make sense of it. The entire code reads as follows:

import Cocoa

let team = ["Gloria", "Suzanne", "Piper", "Tiffany", "Tasha"]
let sortedTeam = team.sorted()
print(sortedTeam)

func captainFirstSorted(name1: String, name2: String) -> Bool {
    if name1 == "Suzanne" {
        return true
    } else if name2 == "Suzanne" {
        return false
    }

    return name1 < name2
}

let captainFirstTeam = team.sorted(by: captainFirstSorted)
print(captainFirstTeam)

In this example, we're sorting team by our custom function using two strings (name1 & name 2) as parameters, but I can't figure out what the function is actually doing to sort our desired name to the front of the list.

First - Is the function passing in all of the string in our team array and comparing them against each other? or just the first two in the array since there are only two parameters?

Second - why would we want to return a Bool in this function? Is it because the 'return name1 < name2' will only trigger if name1 == "Suzanne"? Will a return of false trigger the function to exit?

Hope my questions make sense - Thanks in advance for the help!

1      

Cory wonders about sorting logic.

I can't figure out what the function is actually doing to sort our desired name to the front of the list.

You and I have two jobs. I am in charge of an array of integers. Your job is only to compare two of them. If I handed you two integers and asked you, true or false, is the integer in your left hand less than the integer in your right hand, you would say true, if your left held 7 and your right contained a 42.

I would take your answer and in the background would rearrange my list of integers. Then I would give you two more and ask the same question. You reply true or false accordingly. I continue handing you two integers, and you continue responding until I have successfully sorted the entire list. Your part of the deal is to only tell me, true or false, if the left side is lower than the right.

This same logic works for strings. True or false? Is Newton greater than Pascal? Is Rey greater than Kylo?

However, you can mix the rules up ever so slightly. If you always want Suzanne to be at the top of the list, then no matter what the other name is, you'll consider that Suzanne < OtherName = true. This will be true for any value you're given. Suzanne will ALWAYS be sorted lower. You have to adjust your return value depending on whether Suzanne is in your left, or your right hand.

I will hand you two names, you tell me, true or false, if the name in your left hand is less than the name in your right. You just answer this one question. I will arrange the list of names in the background. I keep handing you names, you keep comparing the two, and eventually you'll get a list of names in order, but with Suzanne at the top o' the list.

PS: Just be aware, this is not a Swift question! This is a logic question. Your journey to 100 Days will have both SwiftUI and logic problems to solve!

Keep coding!

   

Cory ask for clarification:

Is the function passing in all of the string in our team array and comparing them against each other?

This is a great question. This is how procedural programmers think. Give me the WHOLE LIST and I will chonk through the list one entry at a time hammering a new array until I've completed my mission.

But Swift and SwiftUI takes a more functional programming approach. You'll see small functions everywhere. Get used to it. (These are also called closures!)

In this case, you have an array of names that you want sorted. Your procedural instinct is to parse the whole array one or two names at a time, and possibly place them into a new array until the entire array is sorted.

In functional programming, the array has a convenient method named sorted(by: CLOSURE)

Here you just provide a simple function that only compares two items in an array. This is the sorting rule. The array itself takes care of handing out two values to compare. It also figures out how to temporarily store values until the entire array is properly sorted.

See-> Sorting Arrays

Review this documentation until the signature becomes familiar to you. This tells you the parameter you are providing is a function that takes two Elements (could be Ints? could be String? could be VarsityFootballPlayers?) and returns true or false. You provide the rule, the array takes care of the rest!

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, 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.

Save 50% on all our books and bundles!

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.