(Using my usual playing card example)
I am trying to make a generic CardCollection that both a Deck and a Hand would inherit from. Both Decks and Hands would need to be sorted or shuffled, but there would be some differences such as initialisation and whether the method for removing a Card for use elsewhere is Deal (for a Deck), Play, or Discard (for Hands).
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
}
The way I'm currently implementing it (see above) is with a Class that contains an Array of Cards, but I can't use my classes like they were Arrays without writing tons of pass-through functions to get my classes to conform to all the protocols as an Array.
What I need is a way that lets me do things like for card in deck (as if deck were simply an Array<Card>) without writing tons and tons of wrapper functions just to get the CardCollection to conform to all the necessary protocols.
How do I make a CardCollection that functions like it's just an Array<Card> without making pass-through functions on every function used by the protocols that Array uses?