I have
@interface PlayerDeck : Deck
@interface MasterDeck : Deck
Both PlayerDeck and MasterDeck have
@property (nonatomic, strong) NSMutableArray *cards; // All cards in this Deck
Inside Deck’ I’d like to
-(NSString*) descriptionOfDeck: (Deck *) deck
Inside both PlayerDeck and MasterDeck i’d like to
-(NSString*) description{
[super descriptionOfDeck:self];
}
Inside Deck i will iterate over cards inside each individual Decks:
-(NSString*) descriptionOfDeck: (Deck *) deck {
NSString *deckDescription = [[NSString alloc] init];
for (Card *c in [deck cards]) {
deckDescription = [deckDescription
stringByAppendingString:[c description]];
}
return deckDescription;
}
The problem: [deck cards] does not resolve.
Please help me understand.
If I understand correctly, you’re saying the
cardsproperty is only defined on theMasterDeckandPlayerDeckclasses, and not onDeck.If that’s correct, consider the type of
deckwhen you attempt to send it thecardsmessage: it is of typeDeck, which doesn’t definecards. You need to either cast it to a type which does definecards, or definecardsin the base class directly. The latter seems more sensible, both for resolving this issue, and because it seems appropriate that allDecks have cards!Update: An example of casting in your case would be:
But you can see the problem with that, can’t you? It requires the base class to somehow know about its child classes, which sort of defeats the purpose of inheritance.
As I – and @rgeorge, in the comments – have suggested, declaring the
cardsproperty in the base class is the better move.