Swift Complex Data Types

Posted on Mon 10 February 2020 in swift

Yay, on to day 2 of 100 Days of Swift!

I'd forgotten that dictionary literals in Swift are written with square brackets, instead of with curly braces like pretty much every other language that I've used.

var languages = [
    "Alex": ["English", "Italian", "Japanese"],
    "Skyler": ["English", "German", "Latin"],
    "Hal": ["English", "German"],

I'd also forgotten the syntax for using a default value when fetching from a dictionary. This seems awkward to me, Python's defaultdict seems more helpful to me, where the default is stored on the dictionary instead of being specified each time lookup is performed.

languages["Wally", default: ["English"]]

I also have to remember that instantiating an enum using a rawValue produces an optional, no compile time safety there.