我正在尝试完成 iTunes U 上当前可用的目标 c 课程的作业 2。据我所知,我已经从幻灯片中正确输入了所有代码,但在运行时仍然出现错误。我在唯一的错误标志周围添加了几个 NSLog 输出,我必须确切地看到发生了什么。该应用程序编译良好,但在运行时遇到错误。
看起来循环运行正常,直到它最后一次运行。我不知道如何打破它。
生成的日志如下。
2013-03-21 08:30:55.604 Matchismo[5897:c07] I Hate you this much 1
2013-03-21 08:30:55.802 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.803 Matchismo[5897:c07] I Hate you this much 2
2013-03-21 08:30:55.803 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.804 Matchismo[5897:c07] I Hate you this much 3
2013-03-21 08:30:55.804 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.804 Matchismo[5897:c07] I Hate you this much 4
2013-03-21 08:30:55.805 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.805 Matchismo[5897:c07] I Hate you this much 5
2013-03-21 08:30:55.806 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.806 Matchismo[5897:c07] I Hate you this much 6
2013-03-21 08:30:55.807 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.807 Matchismo[5897:c07] I Hate you this much 7
2013-03-21 08:30:55.807 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.807 Matchismo[5897:c07] I Hate you this much 8
2013-03-21 08:30:55.808 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.808 Matchismo[5897:c07] I Hate you this much 9
2013-03-21 08:30:55.808 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.809 Matchismo[5897:c07] I Hate you this much 10
2013-03-21 08:30:55.809 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.809 Matchismo[5897:c07] I Hate you this much 11
2013-03-21 08:30:55.810 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.810 Matchismo[5897:c07] I Hate you this much 12
2013-03-21 08:30:55.810 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.811 Matchismo[5897:c07] I Hate you this much 13
2013-03-21 08:30:55.811 Matchismo[5897:c07] I love you like this, ♥
2013-03-21 08:30:55.811 Matchismo[5897:c07] I Hate you this much 1
2013-03-21 08:30:55.811 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.812 Matchismo[5897:c07] I Hate you this much 2
2013-03-21 08:30:55.812 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.812 Matchismo[5897:c07] I Hate you this much 3
2013-03-21 08:30:55.813 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.813 Matchismo[5897:c07] I Hate you this much 4
2013-03-21 08:30:55.814 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.814 Matchismo[5897:c07] I Hate you this much 5
2013-03-21 08:30:55.815 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.815 Matchismo[5897:c07] I Hate you this much 6
2013-03-21 08:30:55.815 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.816 Matchismo[5897:c07] I Hate you this much 7
2013-03-21 08:30:55.816 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.817 Matchismo[5897:c07] I Hate you this much 8
2013-03-21 08:30:55.817 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.817 Matchismo[5897:c07] I Hate you this much 9
2013-03-21 08:30:55.818 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.818 Matchismo[5897:c07] I Hate you this much 10
2013-03-21 08:30:55.818 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.819 Matchismo[5897:c07] I Hate you this much 11
2013-03-21 08:30:55.819 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.820 Matchismo[5897:c07] I Hate you this much 12
2013-03-21 08:30:55.820 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.820 Matchismo[5897:c07] I Hate you this much 13
2013-03-21 08:30:55.821 Matchismo[5897:c07] I love you like this, ♦
2013-03-21 08:30:55.821 Matchismo[5897:c07] I Hate you this much 1
2013-03-21 08:30:55.822 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.822 Matchismo[5897:c07] I Hate you this much 2
2013-03-21 08:30:55.822 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.823 Matchismo[5897:c07] I Hate you this much 3
2013-03-21 08:30:55.823 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.824 Matchismo[5897:c07] I Hate you this much 4
2013-03-21 08:30:55.824 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.824 Matchismo[5897:c07] I Hate you this much 5
2013-03-21 08:30:55.825 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.825 Matchismo[5897:c07] I Hate you this much 6
2013-03-21 08:30:55.825 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.826 Matchismo[5897:c07] I Hate you this much 7
2013-03-21 08:30:55.826 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.826 Matchismo[5897:c07] I Hate you this much 8
2013-03-21 08:30:55.827 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.827 Matchismo[5897:c07] I Hate you this much 9
2013-03-21 08:30:55.827 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.827 Matchismo[5897:c07] I Hate you this much 10
2013-03-21 08:30:55.828 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.834 Matchismo[5897:c07] I Hate you this much 11
2013-03-21 08:30:55.834 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.835 Matchismo[5897:c07] I Hate you this much 12
2013-03-21 08:30:55.835 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.835 Matchismo[5897:c07] I Hate you this much 13
2013-03-21 08:30:55.836 Matchismo[5897:c07] I love you like this, ♠
2013-03-21 08:30:55.836 Matchismo[5897:c07] I Hate you this much 1
2013-03-21 08:30:55.836 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.837 Matchismo[5897:c07] I Hate you this much 2
2013-03-21 08:30:55.837 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.837 Matchismo[5897:c07] I Hate you this much 3
2013-03-21 08:30:55.838 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.838 Matchismo[5897:c07] I Hate you this much 4
2013-03-21 08:30:55.838 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.839 Matchismo[5897:c07] I Hate you this much 5
2013-03-21 08:30:55.839 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.839 Matchismo[5897:c07] I Hate you this much 6
2013-03-21 08:30:55.839 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.840 Matchismo[5897:c07] I Hate you this much 7
2013-03-21 08:30:55.840 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.840 Matchismo[5897:c07] I Hate you this much 8
2013-03-21 08:30:55.841 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.841 Matchismo[5897:c07] I Hate you this much 9
2013-03-21 08:30:55.841 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.841 Matchismo[5897:c07] I Hate you this much 10
2013-03-21 08:30:55.842 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.842 Matchismo[5897:c07] I Hate you this much 11
2013-03-21 08:30:55.842 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.843 Matchismo[5897:c07] I Hate you this much 12
2013-03-21 08:30:55.843 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.843 Matchismo[5897:c07] I Hate you this much 13
2013-03-21 08:30:55.843 Matchismo[5897:c07] I love you like this, ♣
2013-03-21 08:30:55.844 Matchismo[5897:c07] -[PlayingCard isFaceUp]: unrecognized selector sent to instance 0x8670610
2013-03-21 08:30:55.845 Matchismo[5897:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PlayingCard isFaceUp]: unrecognized selector sent to instance 0x8670610'
*** First throw call stack:
(0x1c96012 0x10d3e7e 0x1d214bd 0x1c85bbc 0x1c8594e 0x2dd9 0x2a8e 0xb015be 0xb010e7 0xb2bb58 0x41de1a 0x41de55 0x10e7663 0x1c9145a 0x233b1c 0xf87e7 0xf8dc8 0xf8ff8 0xf9232 0x483d5 0x4876f 0x48905 0x51917 0x1596c 0x1694b 0x27cb5 0x28beb 0x1a698 0x1bf1df9 0x1bf1ad0 0x1c0bbf5 0x1c0b962 0x1c3cbb6 0x1c3bf44 0x1c3be1b 0x1617a 0x17ffc 0x259d 0x24c5)
libc++abi.dylib: terminate called throwing an exception
(lldb)
扑克牌甲板.m
//
// PlayingCardDeck.m
// Matchismo
//
// Created by James on 3/13/13.
// Copyright (c) 2013 James. All rights reserved.
//
#import "PlayingCardDeck.h"
#import "PlayingCard.h"
@implementation PlayingCardDeck
- (id)init
{
self = [super init];
if (self) {
for (NSString *suit in [PlayingCard validSuits]) {
for (NSUInteger index = 1; index <= [PlayingCard maxRank]; index++) {
PlayingCard *card = [[PlayingCard alloc] init];
card.rank = index;
card.suit = suit;
NSLog(@"I Hate you this much %d", card.rank);
[self addCard:card atTop:YES]; //error is here
NSLog(@"I love you like this, %@", card.suit);
}
}
}
return self;
}
@end
错误是黄色三角形,上面写着:发送'PlayingCard *_strong parameter of type'Card*'的不兼容指针类型
扑克牌.h
//
// PlayingCard.h
// Matchismo
//
// Created by James on 3/13/13.
// Copyright (c) 2013 James. All rights reserved.
//
#import "Card.h"
@interface PlayingCard : NSObject
@property (strong, nonatomic) NSString *suit;
@property (nonatomic) NSUInteger rank;
+ (NSArray *)validSuits;
+ (NSUInteger)maxRank;
@end
扑克牌.m
//
// PlayingCard.m
// Matchismo
//
// Created by James on 3/13/13.
// Copyright (c) 2013 James. All rights reserved.
//
#import "PlayingCard.h"
@implementation PlayingCard
- (int)match:(NSArray *)otherCards
{
int score = 0;
if ([otherCards count] == 1) {
PlayingCard *otherCard = [otherCards lastObject]; // lastObject never goes out of bounds with an Array
if ([otherCard.suit isEqualToString:self.suit]) {
score = 1;
} else if (otherCard.rank == self.rank) {
score = 4;
}
}
return score;
}
- (NSString *)contents
{
NSArray *rankStrings = [PlayingCard rankStrings];
return [rankStrings[self.rank] stringByAppendingString:self.suit];
}
@synthesize suit = _suit;
+ (NSArray *)validSuits
{
static NSArray *validSuits = nil;
if (!validSuits) validSuits = @[@"♥",@"♦",@"♠",@"♣"];
return validSuits;
}
+ (NSArray *)rankStrings
{
static NSArray *rankStrings = nil;
if (!rankStrings) rankStrings = @[@"?",@"A",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"J",@"Q",@"K"];
return rankStrings;
}
- (void)setSuit:(NSString *)suit
{
if ([[PlayingCard validSuits] containsObject:suit]){
_suit = suit;
}
}
- (NSString *)suit
{
return _suit ? _suit : @"?";
}
+ (NSUInteger)maxRank { return [self rankStrings].count-1; }
- (void)setRank: (NSUInteger) rank
{
if (rank <= [PlayingCard maxRank]) {
_rank = rank;
}
}
@end
添加了以下内容:
卡片.h
//
// Card.h
// Matchismo
//
// Created by James on 3/13/13.
// Copyright (c) 2013 James. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Card : NSObject
@property (strong, nonatomic) NSString * contents;
@property (nonatomic, getter=isFaceUp) BOOL faceUp;
@property (nonatomic, getter=isUplayable) BOOL uplayable;
- (int)match:(NSArray *)otherCards;
@end
卡.m
//
// Card.m
// Matchismo
//
// Created by Isao Ishibashi on 2/3/13.
// Copyright (c) 2013 Isao's Awesome App Store. All rights reserved.
//
#import "Card.h"
@implementation Card
- (int)match:(NSArray *)otherCards
{
int score = 0;
for(Card *card in otherCards) {
if ([card.contents isEqualToString:self.contents]) {
score = 1;
}
}
return score;
}
//added
- (NSString*) contents
{
return @"?";
}
@end
甲板.h
//
// Deck.h
// Matchismo
//
// Created by James on 3/13/13.
// Copyright (c) 2013 James. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Card.h"
@interface Deck : NSObject
- (void)addCard:(Card *)card atTop:(BOOL)atTop;
- (Card *)drawRandomCard;
@end
甲板.m
//
// Deck.m
// Matchismo
//
// Created by James on 3/13/13.
// Copyright (c) 2013 James. All rights reserved.
//
#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