swift - 初始化程序委托(delegate)的要点是什么?

标签 swift initialization

我很难理解初始化委托(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/

相关文章:

swift - 为什么 Swift JSONDecodable 会失败?

c# - 当您可以在构造函数中初始化时,为什么需要显式 Initialize 方法?

C结构体使用前要初始化吗?

c# - 如何在 C# 中将自动属性初始化为非空?

c++ - 等号对大括号初始化有影响吗?例如。 'T a = {}' 与 'T a{}'

objective-c - 在 xcode 标签中创建日期

ios - Swift scrollToRow 然后突出显示该行

ios - 第一次点火后的延迟计时器

ios - 单击时 UILabel 点击手势不起作用

javascript - 查询字符串发送值 = 0,不是 jQuery 中的新赋值