簡體   English   中英

如何為特定類型的數組擴展數組功能?

[英]How to extend Array functionality for arrays of a specific type?

(使用我通常的撲克牌示例)
我正在嘗試制作一個普通的CardCollectionCardCollection DeckHand都會從中繼承。 兩個卡座和雙手將需要進行排序或改組,但仍然會有一些差別,如初始化以及是否用於去除方法Card其他地方使用的Deal (對於Deck ), PlayDiscard (對於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM