Learning Swift - Day one

Done

The Goal

I want to start learning and build more mobile apps with Swift. My experince with React native over the last many year has taught me a lot but I feel I’m reaching a point where I need to grow and want to get closer to the device. Ontop of this I have found the “build once run everywhere” isn’t possible if you want to build amazing apps that run well on the users devices. So here begins my journey to become an expert at Swift.


Week 1 - Model, UI & Swift Type System

https://www.youtube.com/watch?v=B42CuI0RO7Y&list=PLoROMvodv4rPHblRXKsJCQs8TLGpiCTrG&index=3

I’ve started watching the computer science course on Youtube I’ll begin this blog from leacture 3 as the first two where very basic and things I had already picked up from React native.

Facts about structs?


struct vs class


Functions in swift

  func multiply(operand: Int, by: Int) -> Int {
    return operand * by
  }

  multiply(operand: 5, by: 5)

OR

   // The "_" here means "nothing here"
   func multiply(_ operand: Int, by otherOperand: Int) -> Int {
       return operand * otherOperand
   }

   multiply(5, by: 6)

Initializers in Swift

 struct RoundedRectangle {
    init(cornerRadius: CGFloat) {
        // Initialize this rectangle with that cornerRadius
    }

    init(cornerSize: CGSize) {
        // initialize this rectangle with that cornerSize
    }
 }

Enum

 enum FastFoodMenuItem {
    case hamburger
    case fries
    case drink
    case cookie
 }
   switch menuItem {
       case .hamburger(let pattyCount):
           print("A burger with \(pattyCount) patties!")
       case .fries(let size): print("a \(size) order of fries")
       case .drink(let brand, let ounces):
           print("a \(ounces)oz \(brand)")
   }
    // The CaseIterable allows us to call the .allCases
    enum TeslaModel: CaseIterable {
        case X
        case S
        case Three
        case Y
    }

    // Because we've done `CaseIterable` above.
    for model in TeslaModel.allCases {
        reportSalesNumber(for: model)
    }

    func reportSalesNumbers(for model: TeslaModel) {
        switch model { ... }
    }

Generics

Optionals

    enum Optional<T> { // T is a generic type, like element in Array<Element>
        case none
        case some(T) // The some case has associated value of that generic type T
    }
    let colors = [Color.blue, .red, .green, .yellow]
    let index: Optional<Int> = colors.firstIndex(of: Color.orange)
    firstIndex(of:) // searches an array to find the given element and returns the index to it. index must be of type Optional<Int> here. Because Color.orange may not be there.
    var hello: String? // var hello: Optional<String> = .none
    var hello: String? = "hello" // var hello: Optional<String> = .some("hello")
    var hello: String? = nil // var hello: Optional<String> = .none
    let hello: String? = ...
    print(hello!) // ! says force get the value and crash if its not there. Extreme case.

    if let safeHello = hello {
        print(safeHello)
    } else {
        // do something else.
    }
    struct Vehicle {
        init?(argument: Type) {
            // initialize my cars as usual, but return nil if you cannot do so for some reason.
        }
    }

    // Which would then have to be called like:
    if let car = Vehicle(type: "Car") {
        // Do something
    }

Exensions

    extension Color {
        var name: String
    }