來自:https : //docs.swift.org/swift-book/LanguageGuide/Initialization.html
每當通用初始化模式的快捷方式將節省時間或使類的初始化意圖更清晰時,就創建便利初始化器。
我對便利 init 的理解基本上是作為“典型”輸入引數的快捷方式。大多數情況下,引數的數量convenience init()與required init()
例如:
public required init() {
super.init()
self.$timeStamp.owner = self
self.$position = self
self.$distance = self
}
public convenience init(timeStamp: FitTime? = nil,
position: Position? = nil,
distance: Measurement<UnitLength>? = nil {
self.init()
self.timeStamp = timeStamp
self.position = position
self.distance = distance
}
}
我相信在某些情況下convenience init,引數數量較少,required init因為一些默認值將被設定。
我認為以下有效?中有一個附加引數,required init但在 中不存在convenience init。但是,在這種情況下,我如何才能訪問并將值傳遞給speed引數?
public required init() {
super.init()
self.$timeStamp.owner = self
self.$position = self
self.$distance = self
self.$speed = self // ADDED THIS
}
public convenience init(timeStamp: FitTime? = nil,
position: Position? = nil,
distance: Measurement<UnitLength>? = nil {
self.init()
self.timeStamp = timeStamp
self.position = position
self.distance = distance
}
}
uj5u.com熱心網友回復:
指定(或什至必需)初始化器和便利初始化器之間的引數數量沒有相關性。
便利初始化器只是那些需要將初始化轉發到指定初始化器之一的初始化器。它有多少輸入引數完全取決于實作和用例。它可能更多、更少或相等。
很難找到一個足夠簡單的例子來演示所有這些,但請考慮這樣的事情:
class HighlightedWordContainer {
let words: [String]
var highlightedWord: String
init(words: [String], highlighted: String) {
self.words = words
self.highlightedWord = highlighted
}
init(words: [String], highlightedIndex: Int) {
self.words = words
self.highlightedWord = words[highlightedIndex]
}
convenience init(singleWord: String) {
self.init(words: [singleWord], highlighted: singleWord)
}
convenience init(word1: String, word2: String, word3: String, highlighted: String) {
self.init(words: [word1, word2, word3], highlighted: highlighted)
}
convenience init(wordsSeparatedByWhitespace: String, highlightedIndex: Int) {
let words = wordsSeparatedByWhitespace.components(separatedBy: .whitespaces)
self.init(words: words, highlightedIndex: highlightedIndex)
}
convenience init?(descriptor: [String: Any], keys: [String], selectedKey: String) {
let words: [String] = keys.compactMap { descriptor[$0] as? String }
guard words.isEmpty == false else { return nil }
guard let selectedWord = descriptor[selectedKey] as? String else { return nil }
guard let selectedWordIndex = words.firstIndex(of: selectedWord) else { return nil }
self.init(words: words, highlightedIndex: selectedWordIndex)
}
}
在這里,我創建了一個帶有 2 個指定初始值設定項的類。這兩個需要設定所有默認還沒有設定的屬性。這意味著他們需要同時設定words和highlightedWord。指定的構造器可能不會將呼叫委托給另一個指定的構造器,因此以下將不起作用:
init(words: [String], highlightedIndex: Int) {
self.init(words: words, highlighted: words[highlightedIndex])
}
并且所有便利初始化器都需要呼叫任何指定的初始化器,并且self在呼叫指定的構造器之前也可能不會直接設定或使用屬性。所以:
convenience init(wordsSeparatedByWhitespace: String, highlightedIndex: Int) {
let words = wordsSeparatedByWhitespace.components(separatedBy: .whitespaces)
// print(highlightedWord) // NOT OK!
self.init(words: words, highlightedIndex: highlightedIndex)
print(highlightedWord)
}
我希望從這個例子中可以清楚地看出,便利初始化程式可以有更多、更少或相同數量的輸入引數。這一切都取決于。
一些更合理的例子:
class Point {
let x: Int
let y: Int
init(x: Int, y: Int) { self.x = x; self.y = y }
convenience init(x: Int) { self.init(x: x, y: 0) }
convenience init(y: Int) { self.init(x: 0, y: y) }
convenience init(polar: (radius: Double, angle: Double)) { self.init(x: Int(cos(polar.angle)*polar.radius), y: Int(sin(polar.angle)*polar.radius)) }
}
class NumericValueAsString {
let stringValue: String // A value represented as "123.456"
init(stringValue: String) { self.stringValue = stringValue }
convenience init(value: Int) { self.init(stringValue: .init(value)) }
convenience init(integerPart: String, fractionPart: String) { self.init(stringValue: integerPart "." fractionPart) }
}
還; required關鍵字與此背景關系中的任何內容無關。你可以把它放在任何一個初始化器(指定的或方便的)、多個甚至所有的初始化器中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/402332.html
標籤:
上一篇:如何使此發布者擴展通用
