类“PlayingCardDeck”继承“Deck”类不能放方法
- (void) addCard:(Card *)card atTop:(BOOL)atTop
Xcode指责我出现以下错误: 没有已知的选择器'addCard:atTop'
的类方法课程:PlayingCardDeck
#import "PlayingCardDeck.h"
#import "PlayingCard.h"
@implementation PlayingCardDeck
- (id)init
{
self = [super init];
if(self) {
for(NSString *suit in [PlayingCard validSuits]){
for (NSUInteger rank = 1; rank <= [PlayingCard maxRank]; rank++){
PlayingCard *card = [[PlayingCard alloc] init];
card.rank = rank;
card.suit = suit;
[PlayingCardDeck addCard:card atTop:NO];
}
}
}
return self;
}
@end
课程:甲板
#import "Deck.h"
@interface Deck()
@property (strong, nonatomic) NSMutableArray *cards;
@end
@implementation Deck
- (NSMutableArray *)cards
{
if(!_cards) _cards = [[NSMutableArray alloc] init];
return _cards;
}
- (void) addCard:(Card *)card atTop:(BOOL)atTop
{
if(atTop){
[self.cards insertObject:card atIndex:0];
} else {
[self.cards addObject:card];
}
}
- (Card *)drawRandomCard
{
Card *randomCard = nil;
if(self.cards.count){
unsigned index = arc4random() % self.cards.count;
randomCard = self.cards[index];
[self.cards removeObjectAtIndex:index];
}
return randomCard;
}
@end
谢谢大家的帮助。
答案 0 :(得分:3)
方法-addCard:atTop:
是实例方法。它必须发送到类的实例,而不是类本身。这一行:
[PlayingCardDeck addCard:card atTop:NO];
正在尝试将其发送到类PlayingCardDeck
,而不是该类的任何实例。这就是错误试图告诉你的。
由于该代码位于-init
类的PlayingCardDeck
方法中,您可能打算将邮件发送到self
:
[self addCard:card atTop:NO];