Created
February 9, 2015 10:31
-
-
Save teameh/6a1a40f12e3515970bc0 to your computer and use it in GitHub Desktop.
willSet didSet playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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