探索Swift开发:从安全委托到macOS笔记应用搭建
1. Swift中的安全委托机制
在Swift编程里,委托模式是一种强大的工具,它能够让一个对象将特定任务委托给另一个对象。下面是一个展示安全委托机制的代码示例:
class House {
// The delegate can be any object that conforms
// to the HouseSecurityDelegate protocol
var delegate : HouseSecurityDelegate?
func burglarDetected() {
// Check to see if the delegate is there, then call it
delegate?.handleIntruder()
}
}
class GuardDog : HouseSecurityDelegate {
func handleIntruder() {
print("Releasing the hounds!")
}
}
let myHouse = House()
myHouse.burglarDetected() // does nothing
let theHounds = GuardDog()
myHouse.delegate = theHounds
myHouse.burglarDetected() // prints "Releasing the hounds!"
在这个示例中, Hous
超级会员免费看
订阅专栏 解锁全文
3

被折叠的 条评论
为什么被折叠?



