DEV Community

Odewole Kehinde
Odewole Kehinde

Posted on • Updated on

Day 2 of 100 Days of SwiftUI


This is the second day of learning SwiftUI using Paul Hudson's 100 Days of SwiftUI challenge.
I wrote a summary of Day 1 here.

Today I learned about complex data-types such as


Please find below a summary of my jottings on these topics

  • Arrays - 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. Please note that If you're using Type Annotations, Array data-type is written in square brackets. Use case: When a set of values can contain duplicates and the order matters.
// 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"

// 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

// Creating an empty Array
let ages = [Int]()
// Or
let ages2 = Array<Int>()
  • Sets - 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). Please note that to create a Set, an array must be passed as argument. Use case: 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.
// 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>()
  • Dictionaries - 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.
let statesCapital = [
    "Abia" : "Umuahia",
    "Lagos" : "Ikeja",
    "Adamawa" : "Yola"

// Prints out "Yola"

// Reading a value from a dictionary that doesn't exist returns nil.

// 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>()

Arrays, Sets, and Dictionaries are called collections because they store different values.

  • Tuples - 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. Use case: When you need a fixed collection of related values where each item has a precise position or name.
// 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"
  • Enums - It is used to "group related values" that makes them usable easily. Use case: 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.
// 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

I'm on to Day3!

Top comments (0)