2013-03-09 2 views
-1

Je reçois l'erreur incompatible pointer types assigning to Deck *__strong from PlayCards *Obtenir une erreur sur mon (premier) CardGameViewController

Et je ne sais pas pourquoi ça. Son dans la première méthode mis en œuvre (plate-forme):

#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 

Ceci est le fichier d'en-tête (rien qui se passe ici):

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

@interface CardGameViewController : UIViewController 


@end 

Et la classe carte à jouer héritant de la classe de plate-forme ..

c'est le 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 
+0

La synthèse est-elle manquante ou l'avez-vous simplement omise? – cardmagik

+0

À quoi ressemble l'initialiseur de PlayingCards? – CodaFi

+0

Je l'ai laissé .. @ cardmagik – JohnBigs

Répondre

4

Cette ligne:

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

devrait être:

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

Ou la propriété 'deck' devrait être' PlayingCards' au lieu de 'Deck'. – rmaddy

+0

mais PlayingCards hérite de Deck, alors pourquoi ne pas ce travail? J'ai besoin des PlayingCards (c'est un jeu de cartes à jouer) alloué et initialisé @rmaddy – JohnBigs

+1

Non, 'PlayingCards' hérite de' Card'. – rmaddy

0

Si le parent pour la carte est de classe NSObject comme vous le dites, et étant donné que PlayingCards hérite de la carte, vous pouvez Ne pas affecter une instance de PlayingCards à une variable de type Deck*. C'est ce que le compilateur vous dit.

Si vous avez vraiment besoin de le faire, vous devez écrire:

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

Ce ne serait valable que parce que dans Objective-C la mise en œuvre est donnée lors de l'exécution et de méthode dont la classe est appelée seulement décidé à l'exécution lorsque le message est envoyé. Cependant, ce modèle est très inhabituel et il vaut mieux être certain que PlayingCards implémente tous les sélecteurs qui peuvent être appelés sur une instance Deck. Une meilleure façon serait d'utiliser des protocoles.

Vous pouvez définir un protocole, puis utiliser:

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

Mettre dans le protocole tous les sélecteurs dont vous avez besoin. Pourquoi ne pouvez-vous pas utiliser ceci?

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

c'est la ligne instructeur http://d.pr/i/IcFp voir si vous êtes en mesure de voir ce @jean – JohnBigs

+0

Im juste essayer d'obtenir lu de l'erreur .. @ Jean – JohnBigs

+1

@Jean Utiliser un casting comme vous l'avez fait juste pour se débarrasser d'un avertissement du compilateur est mauvais. Il va juste planter finalement à l'exécution. – rmaddy

Questions connexes