“No known class method for selector 'validSuit

2019-08-12 05:08发布

问题:

I'm trying to follow along with a Stanford CS193p course, but I'm getting an objective C error that I cannot resolve.

I'm getting the error in the below PlayingCardDeck.m file on the line that contains "[PlayingCard validSuits]".

#import "PlayingCard.h"
#import "PlayingCardDeck.h"

@implementation PlayingCardDeck

- (instancetype)init {
    self = [super init];

    if (self){
        for (NSString *suit2 in [PlayingCard validSuits]) {   //Error on this line

        }
    }

    return self;
}

@end

Here is the PlayingCardDeck.h file:

#import "Deck.h"

@interface PlayingCardDeck : Deck

@end

Here is the PlayingCard.m file:

#import "PlayingCard.h"

@implementation PlayingCard
- (NSString *)contents {

    NSArray *rankStrings = [PlayingCard rankStrings];
    return [rankStrings[self.rank] stringByAppendingString:self.suit];
}

+ (NSArray *)validSuits {
    return @[@"♣︎", @"♠︎", @"♥︎", @"♦︎"];
}

@synthesize suit = _suit;
- (void)setSuit:(NSString *)suit {
    if ([[PlayingCard validSuits] containsObject:suit]) {
        _suit = suit;
    }
}
- (NSString *)suit {
    return _suit ? _suit : @"?";
}
+ (NSArray *)rankStrings {
    return @[@"?", @"A", @"2", @"3", @"4", @"5",
             @"6", @"7", @"8", @"9", @"10", @"J",
             @"Q", @"K"];
}
+ (NSUInteger)maxRank {
    return [[self rankStrings] count] - 1;
}
- (void)setRank:(NSUInteger)rank {
    if (rank <= [PlayingCard maxRank]) {
        _rank = rank;
    }
}
@end

And the PlayingCard.h file:

#import "Card.h"

@interface PlayingCard : Card

@property (strong, nonatomic) NSString *suit;
@property (nonatomic) NSUInteger rank;

+ (NSArray *)validSuits;
+ (NSUInteger)maxRank;
@end

I'm new to objective C and have no idea what is causing this problem. Or why identical code can work for one person and not for me. Any help is appreciated.

回答1:

I cannot see any problems of the code.

You can try clean your build and re-build it.