Day 2 - Data Structures

Day 2 of 100 days of Swift course by Paul Hudson

I've continued to reinforce my knowledge on basics with more complex data structures. Again there is something further to investergate on the difference between arrays and sets.

Arrays are an ordered set of related data. Use arrays when order or uniqueness is important

Sets are an unordered and unique set of data. Use sets when you need fast access and uniqueness or order is not important.

Tuples allow you to set any value as a way to refer to that item instead of an Int like in arrays. Use tuples when you have data that won't change and is related data. I.E. an address or a persons details. When an item doesn't exist it causes an error.

Dictionaries like tuples allow you to set any value as a way to refer to that item. Dictionaries differ in that when an item doesn't exist it returns nil. You can override the nil when you call for the value. Use dictionaries when you have complex and related data.

let favouriteIceCream = [
    "Sara": "Salted Caramel",
    "Nathan": "Mint Choc Chip"
]
favouriteIceCream["Sara"] // Normal call returns "Salted Caramel"
favouriteIceCream["Kahlia"]// Normal call returns nil
favouriteIceCream["Kahlia", default: "Unknown"]// Override call returns "Unknown"

Enums are away of declaring and restricting values. This helps avoid spelling errors and maintains consistency. Use enums when you have a restricted list of related things I.E. A list of days.

enum WeekDays {
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
}

https://gist.github.com/bitterpilot/7ab3e1d57c16e5592a023b1b1b4f416c#file-day-2-swift