标签:
class Person: NSObject {
var name: String?
var age: Int?
}
var _name: String?
var name: String? {
get {
return _name
}
set {
_name = newValue
}
}
Swift
中以上形式的 getter & setter 很少用var length: Int? {
didSet {
timeStr = String(format: "%02d:%02d:%02d", arguments: [length! / 3600, (length! % 3600) / 60, length! % 60])
}
}
var timeStr: String?
var title: String {
get {
return "Mr " + (name ?? "")
}
}
getter
方法的属性被称为计算型属性,等同于 OC 中的 ReadOnly
属性var title: String {
return "Mr " + (name ?? "")
}
init(dict: [NSObject: AnyObject]) {
name = dict["name"] as? String
age = dict["age"] as? Int
}
deinit {
println("88")
}
标签:
原文地址:http://www.cnblogs.com/Milo-CTO/p/4598854.html