[英]How to extend Array functionality for arrays of a specific type?
(使用我通常的扑克牌示例)
我正在尝试制作一个普通的CardCollection
, CardCollection
Deck
和Hand
都会从中继承。 两个卡座和双手将需要进行排序或改组,但仍然会有一些差别,如初始化以及是否用于去除方法Card
其他地方使用的Deal
(对于Deck
), Play
或Discard
(对于Hand
或多个)。
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
使用的协议使用的每个功能都进行传递功能?
您可以定义从RangeReplaceableCollectionType
继承的CardCollection
协议,以及带有默认实现的协议扩展,以将所有访问方法转发到基础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类中使用必需的Initializers? 。
稍微概括一点,您可以定义一个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]()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.