首先,請注意,在您的問題[Die]
陣列的重複初始化將實例單個Die
例如,此後在陣列中重複參考在此實例5倍(因爲Die
是引用類型)。也就是說,在你的例子中[Die]
的所有成員都強烈地引用了相同的底層Die
實例。因此,如果使用引用類型,請記住避免重複數組初始值設定項。現在
,可以構建提供blueprinted static
骰子供應商的方法,其提供Self
實例的數組,使用一些其他blueprinted初始化的默認實現的協議。
// the 'class' requirement not strictly needed here, but it holds semantic
// value to explain the 'map' call rather than using array:s 'repeating'
// initializer in in the default implementation below
protocol DiceFactory: class {
init(faces: Int, id: Int)
static func dice(_ n: Int) -> [Self]
}
extension DiceFactory {
static func dice(_ n: Int) -> [Self] {
return (1...n).map { Self.init(faces: 6, id: $0) }
}
}
如果您標記您的Dice
類final
(爲什麼final
?參考鏈接Q &下方的),你符合DiceFactory
協議時,有機會獲得這個默認實現直接。
final class Die: DiceFactory {
let id: Int
let faces: Int
init(faces: Int, id: Int) {
self.faces = faces
self.id = id
}
}
let myDice = Die.dice(5)
myDice.forEach { print($0, $0.id) }
/* Die 1
Die 2
Die 3
Die 4
Die 5 */
但是,你有沒有明確輸出Die
的好理由?
參見:
你談論'[模具]''中yahtzeeDice()'或身體的方法簽名?後者可以用'Array'替代,因爲可以推斷出Die的元素類型。 – Hamish
無關:爲什麼'yahtzeeDice()'是一個實例方法?你可以把它變成一個'static' /'class'方法,只需使用'self'而不是'type(of:self)'。 – Hamish
你不能在簽名中使用'[Self]',因爲數組是一個結構體,如果'A:B'那麼'[A]'和'[B]'仍然是不相關的類型。 – Sulthan