Skip to content

Instantly share code, notes, and snippets.

@teameh
Created February 9, 2015 10:31
Show Gist options
  • Save teameh/6a1a40f12e3515970bc0 to your computer and use it in GitHub Desktop.
Save teameh/6a1a40f12e3515970bc0 to your computer and use it in GitHub Desktop.
willSet didSet playground
// Playground - noun: a place where people can play
import UIKit
class Person {
var name: String?
var address: Address? {
willSet(newValue) {
if let newAddress = newValue {
println("Will change address to a new address: \(newAddress.street) \(newAddress.buildingNumber)")
}else{
println("Will change address to a nil value")
}
}
didSet (oldAddress) {
if let newAddress = address {
println("Did change address to a new address: \(newAddress.street) \(newAddress.buildingNumber)")
}else{
println("Did change address to a nil value")
}
}
}
init(name: String) {
self.name = name
}
}
class Address {
var buildingNumber: String
var street: String
init(street: String, buildingNumber: String) {
self.street = street
self.buildingNumber = buildingNumber
}
}
var john = Person(name: "John")
let someAddress = Address(street: "Acacia Road", buildingNumber: "29")
john.address = someAddress
john.address = nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment