2014/07/02

Swift willSet didSet

willSet:變化前做什麼事情
didSet:變化後做什麼事情


//
//  main.swift
//  Test
//
//  Created by CYFang on 7/2/14.
//  Copyright (c) 2014 CYFang. All rights reserved.
//

import Foundation

class Animal : NSObject{
    var name:NSString = "cat"{
    
        //變化前
        willSet{
            println("Animal name is \(name)")
        }
    
        //變化後
        didSet{
            println("Animal name is \(name)")
        }
    }
}


var animal:Animal = Animal()
animal.name="dog"

執行結果:

沒有留言:

張貼留言