1

我正在尝试完成 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
4

2 回答 2

2

好吧,让我们一一解决:

错误是黄色三角形,上面写着:发送'PlayingCard *_strong parameter of type'Card*'的不兼容指针类型

这是一个类型错误。一个类型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.

似乎被困在一个内部循环中

恰恰相反。斯坦福在这里布置的练习实际上是循环遍历并为每套花色添加一种类型的卡片。循环不是问题,您的类型错误是。

于 2013-03-21T15:16:33.243 回答
0

我有完全相同的问题。CodaFi 说的完全正确,当我修复它时解决了这个问题。我的错误地说 Deck,但你的说 NSObject。修改你的阅读:

#import "Deck.h"

@interface PlayingCard : Card  // <==== Instead of NSObject

这一修复纠正了这两个问题。

但是,请将正确答案归功于 CodaFi 而不是我——我只是向您展示了代码,因为我也有它。

我检查了第 2 课的幻灯片,它也这么说,所以我知道我搞砸了。很多代码。

祝你好运。

于 2013-03-25T01:42:48.277 回答