前端之家收集整理的这篇文章主要介绍了
Swift基础篇——setter和getter的使用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
setter和getter
import Foundation
class Student:NSObject {
var address: String?
{
willSet{
print("赋值之前\(newValue)")
}
didSet{
print("赋值之后\(oldValue)")
}
}
var score: Double
{
return 99.0
}
override var description: String
{
let proterties = ["address","score"]
let dict = dictionaryWithValuesForKeys(proterties)
return "\(dict)"
}
}
测试类
import UIKit
class ViewController: UIViewController {
override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
let stu = Student()
stu.address = "深圳"
print(stu.address)
print(stu.score)
print(stu.description)
}
}