在我的(第一个)CardGameViewController上出错

时间:2013-03-09 22:52:44

标签: objective-c

我收到错误incompatible pointer types assigning to Deck *__strong from PlayCards *

我不确定为什么会这样。它在第一种方法中实现(甲板):

#import "CardGameViewController.h"
#import "PlayingCards.h"


@interface CardGameViewController ()
@property (weak, nonatomic) IBOutlet UILabel *cardLabel;
@property (nonatomic) NSUInteger flipsCount;
@property (strong, nonatomic) Deck *deck;
@end


@implementation CardGameViewController




-(Deck *) deck {

    if (!_deck) _deck = [[PlayingCards alloc] init];
    return _deck;
}


-(void) setFlipsCount:(NSUInteger)flipsCount {

    _flipsCount = flipsCount;
    self.cardLabel.text = [NSString stringWithFormat:@"Flips:%d", self.flipsCount];

}


- (IBAction)flipCard:(UIButton *)sender {

    sender.selected = !sender.isSelected;

    self.flipsCount++;

}


@end

这是头文件(这里没有任何内容):

#import <UIKit/UIKit.h>
//#import "Card.h"
//#import "Deck.h"
//#import "PlayingCards.h"

@interface CardGameViewController : UIViewController


@end

继承自Deck类的PlayingCard类..

这是PlayingCards.m

#import "PlayingCards.h"


@implementation PlayingCards

@synthesize suit = _suit;

//modifying the contents getter so it will return array with the ranks and rank+suit 
-(NSString *) contents {

    NSArray *cardsRank = [PlayingCards rankStrings];

    return [cardsRank[self.rank] stringByAppendingString:self.suit];
}

//creating a method to make sure we get validated suits
+(NSArray *) validSuit {

    return @[@"♠",@"♣",@"♥",@"♦"];
}

//creating calss method to validate the rank
+(NSArray *) rankStrings {

    return @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];
}

//creating a new setter for suit to make sure we get the valitated suits, uding the validateSuit method
-(void) setSuit:(NSString *)suit {

    if ([[PlayingCards validSuit] containsObject:suit]) {
        _suit = suit;
    }
}

//creating new getter for suit to make sure its not empty
-(NSString *) suit {

    return _suit? _suit: @"?";
}

//creating a class method to make sure when user set the rank he will will
+(NSUInteger) maxRank {

    return [self rankStrings].count - 1;

}

//creating a new setter to the renk to make sure the rank is validates 
-(void) setRank:(NSUInteger)rank {

    if (rank <= [PlayingCards maxRank]) {

        _rank = rank;
    }
}

@end

PlayingCards.h

#import "Card.h"
#import "Deck.h"

@interface PlayingCards : Card

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

+(NSArray *) validSuit;

+(NSUInteger) maxRank;


@end

2 个答案:

答案 0 :(得分:4)

这一行:

if (!_deck) _deck = [[PlayingCards alloc] init];

应该是:

if (!_deck) _deck = [[PlayingCardDeck alloc] init];

答案 1 :(得分:0)

如果Card的父级是您所说的类NSObject,并且假设PlayingCards继承自Card,则您无法将PlayingCards的实例分配给变量输入Deck*。这就是编译器告诉你的。

如果你真的需要这样做,你必须写:

 if (!_deck) _deck = (Deck*)[[PlayingCards alloc] init];

它只是有效的,因为在Objective-C中,实现是在运行时给出的,并且调用哪个类的方法仅在调度消息时在运行时决定。但是,这种模式非常不寻常,您最好确定PlayingCards正在实现可能在Deck实例上调用的所有选择器。更好的方法是使用协议。

您可以定义协议,然后使用:

id <myProtocol> deck = [[PlayingCards alloc] init];

在协议中输入您需要的所有选择器。

为什么你不能用这个?

PlayingCards* deck = [[PlayingCards alloc] init];