我正在尝试从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)
PlayingCardDeck.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
错误是黄色三角形,内容为:不兼容的指针类型,发送'Card *'
类型的'PlayingCard * _strong参数PlayingCard.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
PlayingCard.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
添加了以下内容:
Card.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
Card.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
加入deck.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
Deck.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
答案 0 :(得分:2)
好的,让我们一个一个地解决这个问题:
错误是黄色三角形,内容为:不兼容的指针类型,发送'Card *'
类型的'PlayingCard * _strong参数
这是类型错误。类型为Card
的对象和类型为PlayingCard
的对象是不兼容的,即使它们(或者我假设)都来自NSObject
。类型继承意味着当你有一个基类T和两个T(S和C)的子类时,S和C是不兼容的类型,因为它们不能安全地相互解析。 Objective-C允许您放弃此类型的安全性,因此解决此问题的方法是重构您的方法以接受任何对象id
,或使PlayingCard
成为Card
的子类而不是仅仅导入其定义。继承由冒号右侧的超类决定,而不是由import语句决定。
由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [PlayingCard isFaceUp]:无法识别的选择器发送到实例0x8670610'
这再次源于类型不兼容。我可以假设Card
类是唯一实现-isFaceUp:
而不是PlayingCard
的类。如果是这样,只需从PlayingCard
继承Card
即可。如果没有,请实施-isFaceUp:
或将其转发到Card
的内部实例。
似乎陷入内循环
恰恰相反。斯坦福在这里所做的练习实际上是在为每件套装添加一种卡片。循环不是问题,你的类型错误是。
答案 1 :(得分:0)
我有完全相同的问题。当我修复它时,CodaFi所说的完全正确并解决了这个问题。我错误地说甲板,但你的NSObject说。修改你的内容:
#import "Deck.h"
@interface PlayingCard : Card // <==== Instead of NSObject
这一个修复纠正了两个问题。
但是,请将CodaFi归功于正确的答案而不是我 - 我只是在向您展示代码,因为我也有。
我检查了第2讲幻灯片,它也说了这些,所以我知道我搞砸了。很多代码。
祝其他人好运。