Alex Does Things Poorly

Alex does a variety of things poorly - swift, data science, datavis, ML, drawing, AR, unity, etc, etc.

10 February 2020

Swift Complex Data Types

by alex

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.