Mohamed Omar

100 Days of SwiftUI - Day 1

Day 1 of Paul Hudson's 100 days of SwiftUI was a quick rundown of some of the basic building blocks of the language, like variables and constants, strings and integers. I found a few similarities with JavaScript — in some cases I forgot I wasn't looking at JavaScript! — and already found a few gotchas that are bound to trip me up in the future.

First impressions

  • Swift is more strict than JavaScript when it comes to types
  • Working in Xcode feels weird after years in VS Code
  • I haven't built a multi-million dollar app yet. What gives?

Variables

There are two ways of declaring variables in Swift: let and var.

Unlike JavaScript's let — which allows for not just changing the variable's value, but also its type — let is a constant in Swift. The language screams at you if you try and do this, which adds to the type safety.

var, meanwhile, feels very similar to JavaScript's var, itself a predecessor to let. The JS version and Swift version appear identical: they allow for changing the variable's value and type.

Strings

Strings in Swift seem fairly harmless so far, with the exception of it being allergic to single quotes. I'm a double quotes guy generally in JavaScript, but I do reach for single quotes a lot when working with template literals or when passing data in Vue (sentence="Here is a single 'quote'") for example.

Swift also allows for multi-line strings, which is nice. To do that, the string has to be sandwiched between triple quotes, like so:

let multi = """
  Swift will
  absolutely
  love this!
"""

Integers

Integers themselves seem straightforward, too, at least for now. Floating point numbers, however — or "Doubles" as Swift refers to them, seem a bit more complicated.

The course's main takeaway for floats and integers is that they cannot be used together, and must be convered to the same type before any calculations can be made. Another example of type safety.

I'm sure this will eventually prove useful once I start actually building things, but it feels a little strange coming from JavaScript world, where there are fewer guard rails for working with mixed types.