属性
存储属性
计算属性
注:不能只有set
枚举rawValue原理
延迟存储属性(Lazy Stored Property)
延迟存储属性注意点
属性观察器(Property Observer)
全局变量、局部变量
inout的再次研究
struct Shape {
var width: Int
var side: Int {
willSet {
print("willSetSide", newValue)
}
didSet {
print("didSetSide", oldValue, side)
}
}
var girth: Int {
set {
width = newValue / side
print("setGirth", newValue)
}
get {
print("getGirth")
return width * side
}
}
func show() {
print("width=\(width), side=\(side), girth=\(girth)")
}
}
func test(_ num: inout Int) {
num = 20
}
var s = Shape(width: 10, side: 4)
test(&s.width)
s.show()
print("----------")
test(&s.side)
s.show()
print("----------")
test(&s.girth)
s.show()
打印结果
getGirth
width=20, side=4, girth=80
----------
willSetSide 20
didSetSide 4 20
getGirth
width=20, side=20, girth=400
----------
getGirth
setGirth 20
getGirth
width=1, side=20, girth=20
inout的本质总结
总结:inout的本质就是引用传递(地址传递)
类型属性(Type Property)
类型属性细节
单例模式
// 单例模式
class FileManager {
public static let shared = FileManager()
private init() { }
func open() {
}
func close() {
}
}
FileManager.shared.open()
FileManager.shared.close()