Day 2 of 100 Days of SwiftUI
Tags: swift, swiftui, beginners, day2
Published: 2020-01-16 22:16:14

#Introduction

This is the second day of learning [SwiftUI](https://developer.apple.com/xcode/swiftui/) using [Paul Hudson](https://twitter.com/twostraws)'s [100 Days of SwiftUI challenge](https://www.hackingwithswift.com/100/swiftui). <br /> [I wrote a summary of Day 1 here](https://dev.to/kennyedward/day-1-of-100-days-of-swiftui-challenge-opo).

Today I learned about <strong>complex data-types</strong> such as 
<ul>
<li><strong><a href="#array">Arrays</a></strong></li> 
<li><strong><a href="#set">Sets</a></strong></li>
<li><strong><a href="#dictionary">Dictionaries</a></strong></li>
<li><strong><a href="#tuple">Tuples</a></strong></li>
<li>and Enumerations (also called <strong><a href="#enum">Enums</a></strong>)</li>
</ul>

#Summary
Please find below a summary of my jottings on these topics
<ul>
  <li>
    <strong id ="array">Arrays</strong> - It is a collection of values that are stored as a single value. Each value seperated by a comma. It starts and ends with a square bracket. Accesing values in the array is done by reference the array index number. Array numeric position starts its numbering from 0. <strong>Please note</strong> that If you're using Type Annotations, Array data-type is written in square brackets. <br /> <strong>Use case:</strong> When a set of values can contain duplicates and the order matters.
  </li>
</ul>
``` swift
// Declaring a variable
let name = "Kehinde Odewole"
let tribe = "Yoruba"
let state = "Lagos"
let country = "Nigeria"
let continent = "Africa"

// Placing these variables in an Array
var userInfo = [name, tribe, state, country, continent]

// Array Type Annotation
var userInfo : [String] = [name, tribe, state, country, continent]

// Prints "Africa"
userInfo[4]

// Re-assigning the variable name 
userInfo[0] = "Taiwo"

// Swift is going to crash (Fatal error: Index out of range) if you attempt to access a value that is out of the array range
//userInfo[5]

// Creating an empty Array
let ages = [Int]()
// Or
let ages2 = Array<Int>()
```

<ul>
  <li>
    <strong id ="set">Sets</strong> - Items listed in Sets cannot occur twice, that is all items listed are unique. Also, items are not index based like arrays. They are stored in an unorder manner (not neccessariy random order). <strong>Please note</strong> that to create a Set, an array must be passed as argument. <br /> <strong>Use case:</strong> When a set of values MUST be unique or you need to quickly check if an item exist. For example all handles on Twitter are unique. 
  </li>
</ul>
``` swift
// values are displayed in unorder manner
let daysOfWeek = Set(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"])

// items listed cannot occur twice. Other occurences of 1 is discarded
// Randomly outputs {5, 1, 3, 4, 6}
let numbers = Set([1, 4, 6, 3, 5, 1, 1, 1])

// Creating an empty Set
let uniqueName = Set<String>()
```

<ul>
  <li>
    <strong id ="dictionary">Dictionaries</strong> - They store different values similar to arrays, but instaed of accessing item via the integer position, you can access them using the key. It uses a { key : value } format like JavaScript's object.
  </li>
</ul>
``` swift
let statesCapital = [
    "Abia" : "Umuahia",
    "Lagos" : "Ikeja",
    "Adamawa" : "Yola"
]

// Prints out "Yola"
statesCapital["Adamawa"]

// Reading a value from a dictionary that doesn't exist returns nil.
statesCapital["Enugu"]

// However, you can return a default value if a key doesn't exist. It is called dictionary default values
statesCapital["Enugu", default: "Unknown"]

// to create an empty dictionary
let surnames = [String : String]()
// or
let surnames2 = Dictionary<Int, String>()
```

<strong>Arrays, Sets, and Dictionaries</strong> are called <strong>collections</strong> because they store different values.

<ul>
  <li>
    <strong id ="tuple">Tuples</strong> - When a tuple is created, you cannot add or remove items from it. They are fixed in size. You can't change data types of items in a tuple. However, you can change or access an item in a tuple by its numerical index position or name like array. <br><strong>Use case:</strong> When you need a fixed collection of related values where each item has a precise position or name.
  </li>
</ul>
``` swift
// Declaring a tuple
var fullName = (first : "Kehinde", middle : "Edward", last : "Odewole")

// Re-assigning the "first" key
fullName.first = "Taiwo"

// Re-assigning the "middle" key
fullName.1 = "Charles"
```

<ul>
  <li>
    <strong id ="enum">Enums</strong> - It is used to <strong>"group related values"</strong> that makes them usable easily. <br /><strong>Use case:</strong> When you have a fixed sets of related values for a particular operation, it is suitable to use enum. For example there can only be 7 days in a week, genres of movies, suits in a deck of cards, months of the year.</li>
</ul>
``` swift
// Declaring an enum
enum Result {
    case success
    case failure
}

// This always returns failure
let result = Result.failure

// Enums can also store associated values attached to each case. This lets us attach additional information to your enums so they can represent more meaningful data.
enum Activity {
    case bored
    case driving(destination : String)
    case singing(volume : Int)
}

let singing =  Activity.singing(volume: 50)

// Sometimes you need to assign values to enums so they have more meaning
// Swift will automatically assign each of those a number starting from 0, and you can use that number to create an instance of the appropriate enum case
enum Days_v1: Int {
    case Monday
    case Tuesday
    case Wednesday
    case Thursday
    case Friday
    case Saturday
    case Sunday
}
// For example Prints Monday because Swift automagically assigns 0 to Monday.
let monday_v1 = Days_v1(rawValue: 0)

 // It’s not very natural for us to think of Monday as day 0, so you could re-write the Days enums like this:
// Now Swift will assign 1 to Monday and count upwards from there, meaning Sunday is the 7th day
enum Days_v2: Int {
    case Monday = 1
    case Tuesday
    case Wednesday
    case Thursday
    case Friday
    case Saturday
    case Sunday
}

// Sunday is the seventh day. Yay!
let monday_v2 = Days_v2(rawValue: 7)
```
Thanks for reading🙏🏿.

You can find me on Twitter [@RealKennyEdward](https://twitter.com/RealKennyEdward)

I'm on to Day3!