Categories
Apple swift

100 Days of SwiftUI

I’ve been dabbling in the Swift programming language lately and after joining the /r/SwiftUI subreddit I discovered a fantastic free online course by Paul Hudson for learning the SwiftUI framework. The basic idea is, spend about an hour a day for 100 days learning about Swift and SwiftUI and then post about it. Slow and steady wins the race.

I’ve been reluctant to write about it because I wanted to try the course out before locking myself into the commitment device of publicly posting about my plan for the next 100 days, however, I just finished Day 6, I love it, and I plan on doing it every day for the next 94 days.

Details of my learning so far after the jump (posted here mostly for learning and review purposes):


Day 1
The first 12 days of SwiftUI are unique to SwiftUI but are general concepts for Swift. Since I’m still just learning Swift I started on Day 1. The concepts (though they sound tricky) were pretty straightforward: variables, simple data types, and string interpolation.

Variables – variables were one of the first things I learned about programming when I first started programming BASIC on my APPLE II compatible computer back in grade 5 (1990).

Simple Data Types – Constants, variables, and other data come in different types and the neat thing about Swift is that one can build his own data types from scratch.

String Interpolation – The ability for Swift to insert the values of variables and constants into strings such as:

"Hello \(name)!"

Not only that, it turns out, you can actually run code inside there as well.

Day 2
arrays — pretty straightforward…
var myArray = ["item 0", "item 1", "item 2"]

dictionaries — Useful… like an array with a key and a corresponding value. Here’s how you would create a dictionary of HTTP response codes and their related messages:
var responseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]

sets — sets are like arrays but sets don’t keep track of the order of the items inside themselves.

enums — I like to think of enums as being like booleans except instead of just true and false there can be any number of items that you set yourself.

enum Sign {
case rock
case paper
case scissors
}

Day 3
operators and conditions —

The hardest one to get my head around was the ternary operator. Ternary operators work with three values at once: it checks a condition specified in the first value, and if it’s true returns the second value, but if its false returns the third value.

let firstValue = 5
let secondValue = 6
print(firstValue == secondValue ? "Values are the same" : "Values are different")

Broken down it’s: If this then that else the other thing.

Day 4
Loops, loops, and more loops — a couple of new things I learned about loop functions… I previously didn’t know about the “loop” command. It runs the loop once before checking the while condition. The “continue” command resets the loop skipping the current iteration but continuing from there.

Day 5
Functions, parameters, and errors — learned about functions, how to pass parameters, and how to write up error codes.

Day 6
Today was a lesson on closures. As far as I can tell, closures are a lot like functions but are stored in a variable or a constant. I wasn’t sure why one would prefer a closure over a function but after a bit of research I found out the benefits of closures. An example:

func travel(action: () -> Void) {
print("I'm getting ready to go.")
action()
print("I arrived!")
}

let drive = "I'm driving"
let fly = "I'm flying"
let sail = "I'm sailing"

travel(action: drive)

Update: I’m just going to keep adding my days here as I complete them.

Tuesday, October 22, 2019 – Day 7
It was closures, part II, today. The questions I had about why someone would use a closure instead of a regular function have been answered. Closures allow variables to be passed back and forth and even modified from a variable. They are a shortcut to writing longer code. Some of the little projects I had in mind to make will be much easier now that I am beginning to understand closures.

Wednesday, October 23, 2019 – Day 8
Structs, properties, and methods were the topics of today’s lesson.

Thursday, October 24, 2019 – Day 9
access control, static properties, and laziness

Today was really Structs day two. I’m not sure I have my head around why if you want to change a property inside a method, you must mark it as mutating. It seems to me that I haven’t had trouble changing properties that aren’t marked as mutating.

The other question concerns the arguement marked in red below:

struct School {
var staffNames: [String]
private var studentNames: [String]
init(staff: String...) {
self.staffNames = staff
self.studentNames = [String]()
}
}
let royalHigh = School(staff: "Mrs Hughes")

I’m not sure what the three dots, “…” means after a string type.

Friday, October 25, 2019 – Day 10
classes and inheritance — Now I understand mutability a little better. Structs that are constants need the mutable keyword to allow changes to variables because the struct itself is a constant. Classes, on the other hand, allow changes to variables, regardless if the created class is a constant.

Saturday, October 26, 2019 – Day 11
Protocols and extensions — I get the feeling protocols and extensions almost allow you to kind of write my own language into Swift meaning that I can create requirements for different types to follow and then use extensions to implement and change those requirements. It’s complicated stuff and I feel like it’s hard to keep it in my head but I also have a feeling that doing the upfront work of creating protocols will make it easier, saving mental concentration down the road.

Sunday, October 27, 2019 – Day 12
optionals, unwrapping, and typecasting

Monday, October 28, 2019 – Day 13
First day of review. Not sure if I’m actually going g to get anything done today though because of the kids.

Tuesday, October 29, 2019 – Day 14
Review again today. I’ve been doing some of my review on the accompanying app.

Wednesday, October 30, 2019 – Day 15
Bad showing again today. The review feels much harder to slog through than the original material.

Thursday, October 31, 2019 – Day 16
First project!