我是iOS开发的新手,并开始使用CS193P(2013年秋季)学习它。当我试图完成作业1时,我遇到了一个奇怪的Thread问题。
发生在PlayingCard.m上,当我在刺激器中点击卡背面时出现了这个问题。
有人可以告诉我出了什么问题,或者我该如何解决这个Thread问题。 the Thread issue
it appear on setRank: method(which exactly the same as the slide)
#import "PlayingCard.h"
@implementation PlayingCard
- (NSString *)contents
{
NSArray *rankStrings = [PlayingCard rankStrings];
return [rankStrings[self.rank] stringByAppendingString:self.suit];
}
@synthesize suit= _suit;
+ (NSArray *)validSuits
{
return @[@"♥",@"♦",@"♠",@"♣"];
}
- (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`
我为作业编写的viewController.m。
`#import "ViewController.h"
#import "PlayingCardDeck.h"
#import "PlayingCard.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property(nonatomic) int flipCount;
@property (strong, nonatomic) Deck *deck;
@end
@implementation ViewController
-(Deck *)deck{
if(!_deck) _deck = [[PlayingCardDeck alloc] init];
return _deck;
}
- (void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipCount];
NSLog(@"flipCount changed to %d", self.flipCount);
}
- (IBAction)touchCardButton:(UIButton *)sender {
if ([sender.currentTitle length]) {
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
}else{
NSString *cardContent = [[self.deck drawRandomCard] contents];
if (cardContent) {
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
}
}
self.flipCount++;
}
@end`