Info

Programmer by day, artist by night

Posts tagged Apple

20180102_011958

MVC Communication Methods

Views use:

  • Outlets – target action
  • Delegates – should, will, did
  • Data Source – data at, count

Models use:

  • Notification & KVO (Key Value Observing)

Additional Notes

The Card struct is UI independent. It’s tempting to allow the Card to also store the emoji symbol. However the emoji symbol is a view element. Therefore it should not be allowed in Card.

Classes get free initializers as long as all their instance variables are initialized.
Struct also get free initializer allowing all its instance variables to be initialized.

Lazy vars cannot have property observers (didSet).

State of the App after Implementing the Code from Lecture 2

Source code

The source code of the app Concentration is available here:
https://github.com/sanjibahmad/Concentration

Apple’s latest language Swift provides some nifty behaviors and characteristics. Three things we are loving about Swift so far are:

  1. Awesome OOP features: Structs, Enums, Classes can all have methods, optional binding, type safety
  2. Cool Functional language features: Closures (the shorthand syntax is a delight to use), nested functions and types
  3. Interesting Cocoa binding behaviors, as if the language was written to build beautiful Cocoa apps

We are getting solid feeling of how Swift was designed to create powerful and elegant programs. Experience will tell more down the line.

Here is a fun experiment with prefix operators. We wrote the following two custom global functions to mimic Lisp’s + prefix operator:

@prefix func + (ints: Int[]) -> Int {
    var sum = 0
    for int in ints {
        sum += int
    }
    return sum
}
@prefix func + (strings: String[]) -> String {
    var sum = ""
    for string in strings {
        sum += string
    }
    return sum
}

+[100, 200, 200]
// returns 500

+["comp", "uter"]
// returns "computer"