Today was Day 4 of 100 Days of Swift, which meant loops.

The first new bit for me today was that unlike in Python, you can’t loop over a tuple in Swift. I’m not sure I’ve ever treated a Python tuple as an iterable, but it’s good to know that you can’t do that in Swift.

Another piece of syntax that was new to me was Swift’s repeat loops:

var wins = 1
repeat {
    wins += 1
} while wins <= 3

repeat {
    print("This will run once")
} while false

Finally, while I probably could have guessed that Swift would support loop labels, I wasn’t quite sure what the syntax would be. Again, this isn’t the kind of thing I’ve used very much, but it’s still nice to get a little clarity on the Swift behavior:

outer: for i in 1...4 {
    for j in 1...4 {
        if String(i*j).count > 1 {
            print("We got double digits")
            break outer

After yesterday’s late night, I’m a little behind on work for MFArtist, so I’ll be spending at least 60 minutes today catching up.

Operators and Conditions

Today was day 3 of the 100 Days of Swift challenge, which meant operators and conditions. Like the past few days, I expected to already know almost everything, but I was delightfully surprised by a few new things.

First of all, I was familiar with using range in for loops, but not in switch statements. This adds to the flexibility of pattern matching in switch statements that I learned about yesterday. It might seem obvious but I’d also never seen ranges as named variables, either.

let score = 85
switch score {
case 0..<50:
    print("You did poorly.")
case 50..<85:
    print("You did ok.")
    print("You did well.")
let passingRange = 70...100

The other thing that was new to me was the fallthrough statement. Unlike most C-base languages, Swift case statements don’t automatically fall through to the next case if there isn’t a break. If you want the C-style behavior, you can get it by adding fallthrough to your case block:

switch weather {
case "fog":
    print("Wear layers, it might be cold later")
case "rain":
    print("Stay home")
case "sunny":
    print("Must be fall")
    print("Enjoy your day!")

It was Oscar’s birthday today so I didn’t have time to work on anything after first 30 minutes. Tomorrow I plan to make up for it by working for an extra 30 minutes on MFArtist.

Complex Datatypes

This morning I’m on Day 2 of 100 Days of Swift. Today I worked through complex datatypes: arrays, dicts, sets, tuples, and enums. I expected to not learn a lot today, but I was surprised by how much I wasn’t already familiar with.

First up, sets and tuples in Swift. While I knew that Swift had sets and tuples, I hadn’t consciously used them before. I’ve used tuples a fair amount in Python, and it was cool to see the Swift syntax was mostly analogous. In Python I’m a fan of namedtuple for lightweight structured data. However, in Swift I imagine you’d want to use structs instead?

Another thing that was new to me was default values for dictionaries. While looking further into them, I also discovered an optional unwrapping syntax with default that was completely new to me:

let favoriteIceCream = [
    "Paul": "Chocolate",
    "Sophie": "Vanilla",
favoriteIceCream["Charlotte", default: "Unknown"]
favoriteIceCream["Charlotte"] ?? "Unknown"

The third thing that was new to me was associated values for enums. I’d come across this while reading about Swift before, but never used it or actively put it into practice.

enum Activity {
    case bored
    case running(destination: String)
    case talking(topic: String)
    case singing(volume: Int)
let footie = Activity.talking(topic: "football")
let crooning: Activity = .singing(volume: 19)

I did a little further exploration, and you can combine associated values with Swift’s switch statement for some cool pattern matching effects:

switch crooning {
case .singing(let volume) where volume > 30:
    print("singing loudly")
case .singing(let volume) where volume <= 30:
    print("singing quietly")

This was my first 30 minutes. With my second 30 minutes I’m going to start working on getting S3 support working for MFArtist again, since the library I had been using hasn’t been updated for Swift 4.2.

Only 98 days left!

100 Days of Swift!

I want to get back into Swift development for work this year, and it’s also useful for game development since I’m mostly interested in iOS game dev anyways. I’ve done some exploring recently with Unity + ARKit and Unity + Google Cardboard for VR, but I’d also like to experiment with Swift for ARKit in the next few months.

Anyways, in order to more formally structure getting back into Swift development, I’m using Paul Hudson’s excellent 100 Days of Swift project. I’ve read a few of his books and took a workshop from him at Forward Swift a few years ago, which helped me get the confidence to make the few apps that I’ve shipped to the App Store that use Swift: Seek (V1) and MFArtist

I’ll be going back to basics for the 100 Days of Swift, which is fine because in the first day I already learned to pretty basic things that I didn’t know before. First, I was vaguely aware of Swift’s multi-line string syntax, but I didn’t know how they worked. I especially like that you can format strings in multi-line style to keep code width down without adding line breaks to the string:

let myStr = """
 This is formatted over \
 multiple lines in the \
 code, but will have no \
 line breaks at runtime.

Another thing that was new to me is using underscores to demarcate thousands in numbers for legibility to humans:

let pi = 3.141_592_653
var californiaPopulation = 40_017_007

That was my first 30 minutes. With my second 30 minutes I’m going to start working on getting MFArtist up-to-date with the latest Swift.