(使用我的常规纸牌示例)
我正在尝试制作CardCollection
aDeck
和aHand
都将继承的泛型。Decks和Hands都需要进行排序或混洗,但是会存在一些差异,例如初始化以及Card
用于其他地方的删除a的方法是Deal
(for Deck
)Play
还是Discard
(for Hand
s)。
class CardCollection: <Some protocol or another that Arrays use> {
var collective = [Card]()
// CardCollection-specific functions
// pass-through functions
func append(newCard: Card) {
collective.append(newCard)
}
}
class Deck: CardCollection {
// deck-specific functions
}
class Hand: CardCollection {
// hand-specific functions
}
我当前实现它的方式(见上文)是使用包含卡片数组的类,但是如果不编写大量的传递函数来使我的类符合我的要求,就无法使用它们像数组那样的类所有协议都作为数组。
我需要的是一种无需编写大量包装函数即可让我遵循所有必要协议的方法for card in deck
(就像deck
只是一个Array<Card>
)而已CardCollection
。
如何在不Array<Card>
使用所使用协议的每个函数上都通过传递函数的情况下,使CardCollection像它只是一个函数Array
?
您可以定义CardCollection
从继承的协议RangeReplaceableCollectionType
,以及具有默认实现的协议扩展,以将所有访问方法转发到基础collective
数组:
struct Card {
// Simplified for demonstration purposes:
let rank : Int
let suit : Int
}
protocol CardCollection : RangeReplaceableCollectionType {
var collective : [Card] { get set }
}
extension CardCollection {
var startIndex : Int { return collective.startIndex }
var endIndex : Int { return collective.endIndex }
subscript(position : Int) -> Card {
get {
return collective[position]
}
set(newElement) {
collective[position] = newElement
}
}
mutating func replaceRange<C : CollectionType where C.Generator.Element == Card>(subRange: Range<Int>, with newElements: C) {
collective.replaceRange(subRange, with: newElements)
}
}
然后
struct Deck: CardCollection {
var collective = [Card]()
}
struct Hand: CardCollection {
var collective = [Card]()
}
都符合RangeReplaceableCollectionType
并且可以像数组一样对待:
var deck = Deck()
deck.append(Card(rank: 1, suit: 1))
deck[0] = Card(rank: 2, suit: 3)
for card in deck {
print(card)
}
var hand = Hand()
hand.append(deck.first!)
如果Deck
/Hand
是类而不是结构,则它们必须是final
或具有required init()
方法,请进行比较。为什么在Swift类中使用必需的初始化器?。
稍微概括一点,您可以定义一个ElementCollection
协议(独立于Card
类型),该协议的行为类似于数组(通过遵循RangeReplaceableCollectionType
),并将访问转发给基础elements
数组:
protocol ElementCollection : RangeReplaceableCollectionType {
typealias Element
var elements : [Element] { get set }
}
extension ElementCollection {
var startIndex : Int { return elements.startIndex }
var endIndex : Int { return elements.endIndex }
subscript(position : Int) -> Element {
get {
return elements[position]
}
set(newElement) {
elements[position] = newElement
}
}
mutating func replaceRange<C : CollectionType where C.Generator.Element == Element>(subRange: Range<Int>, with newElements: C) {
elements.replaceRange(subRange, with: newElements)
}
}
然后用作
struct Card {
// Simplified for demonstration purposes:
let rank : Int
let suit : Int
}
struct Deck: ElementCollection {
var elements = [Card]()
}
struct Hand: ElementCollection {
var elements = [Card]()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句