Learning Swift · Part 7 of 8

Day Seven

Done

The Goal

Today is day 7 of learning Swift. I’ve started off by reading the next document book “Core Foundation”. Here I learnt a lot about functions and how we can use them in all of Swifts different ways. I’ve also started listening to L8 - Animation.


Animation

extension Animation {
    static let name_of_let = Animation.easeInOut(duration: 3)
}

// This means we can call this withAnimation(Animation.easeInOut(duration: 3)) -> withAnimation(Animation.guess) -> withAnimation(.guess)
    .transaction { transcation in
        if isCheck {
            transaction.animation = nil
        }
    }
    Text("HELLO")
        .transition(.offset(x: 0, y: 200))
    extension AnyTransition {
        static let name_of_let AnyTransition.offset(x: 0, y: 100)
    }
    VStack {
        Text("Hello")
        Text("World")
    }
    .transition(
        AnyTransition.asymmetric(insertion: .move(edge: .trailing), removal: .move(edge: .trailing))
    )
    VStack {
        Text("Hello")
        Text("World")
    }
    .transition(
        AnyTransition.asymmetric(insertion: isTrue ? .opacity : .move(edge: .top), removal: .move(edge: .trailing))
    )
    extension AnyTransition {
        static func nameOfFunc(_ isOver: Bool) -> AnyTransition {
            AnyTransition.asymmetric(
                insertion: isOver ? .opacity : .move(edge: .top),
                removal: .move(edge: .trailing)
            )
        }
    }
    VStack {
        Text("Hello")
        Text("World")
    }
    .transition(
        .attempt(game.isOver)
    )
    Button("Restart") {
        // The .restart is from the static we defined above.
        withAnimation(.restart) {
            restarting = true
        } completion: {
            withAnimation(.restart) {
                game.restart()
                selection = 0
            }
        }
    }
    Group {
        Text("Max")
        Text("Hello")
    }

Reading

Subscript

subscript(index: Int) -> ElementType {
    get {

    }

    set(newValue) {

    }
}
struct Matrix {
    let rows: Int, columns: Int
    var grid: [Double]

    subscript(row: Int, column: Int) -> Double {
        get {
            return grid[(row * columns) + column]
        }

        set {
            grid[(row * columns) + column] = newValue
        }
    }
}

// Usage:
var myMatrix = Matrix(rows: 2, columns: 2, grid [1.0, 2.0, 3.0, 4.0])

// Reading a value using the subscript
print(myMatrix[0, 1]) // Outputs: 2.0

// Writing a value using the subscript:
myMatrix[0, 1] = 5.0
print(myMatrix[0, 1]) // Outputs 5.0
Key takeaways

Inheritance

class Vehicle {
    var currentSpeed = 0.0
    var description: String {
        return "travelling at \(currentSpeed) miles per hour"
    }

    func makeNoise() {
        // do nothing
    }
}

// Automatically inherits all of the above.
class Bicycle: Vehicle {
    var hasBasket = false

}
class SomeSubclass: SomeSuperclass {
    // subclass definitions go here.
}
class Tandem: Bicycle {
    var currentNumberOfPassengers = 0

}

Overriding

class Train: Vehicle {
    override func makeNoise() {
        print("Choo Choo")
    }
}

let train = Train()
train.makeNoise()
// prints "Choo Choo"