我很难理解初始化委托(delegate)的意义所在。我目前正在从 Big Nerd Ranch 书中学习 Swift,并遇到了以下代码:
init (region: String, population: Int, stopLights: Int) {
self.region = region
self.population = population
numberOfStoplights = stopLights
}
init (population: Int, stopLights: Int {
self.init(region: “N/A”, population: population, stopLights: stopLights)
}
这让我很困惑。如果初始化一个实例的全部目的是给它一个值,那么你为什么要初始化它两次?
最佳答案
第二个 init() 只是用不同参数初始化类的另一种方法。在此示例中,如果区域未知,您仍然可以使用区域获取默认值“N/A”来初始化类。
关于swift - 初始化程序委托(delegate)的要点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35423933/