1

Itunes U で利用可能な現在の Objective 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

エラーは黄色の三角形で、次のように表示されます: Incompatible pointer types sent 'PlayingCard *_strong parameter of type 'Card*'

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

以下を追加しました。

カード.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

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
4

2 に答える 2

2

了解しました。これらを1つずつ修正しましょう。

エラーは次のような黄色の三角形です。互換性のないポインタタイプが「PlayingCard*_strongパラメータのタイプ」「Card*」を送信しています

これはタイプエラーです。タイプのオブジェクトとタイプCardのオブジェクトはPlayingCard、両方(または私が推測する)がから派生している場合でも、互換性がありませんNSObject。型の継承とは、基本クラスTとTの2つのサブクラス(SとC)がある場合、SとCは互いに安全に解決できないため、互換性のない型であることを意味します。Objective-Cを使用すると、この型安全性を放棄できます。したがって、これを修正する方法は、定義をインポートするだけでなく、メソッドをリファクタリングして任意のオブジェクトを受け入れるか、のサブクラスidを作成することです。継承は、インポートステートメントではなく、コロンの右側にあるスーパークラスによって決定されます。PlayingCardCard

キャッチされなかった例外'NSInvalidArgumentException'が原因でアプリを終了しています、理由:'-[PlayingCard isFaceUp]:認識されないセレクターがインスタンス0x8670610に送信されました'

これもまた、タイプの非互換性に起因します。クラスは、ではなく、Cardを実装する唯一のクラスであると推測できます。その場合は、から継承するだけです。そうでない場合は、それを実装するか、の内部インスタンスに転送します。-isFaceUp:PlayingCardPlayingCardCard-isFaceUp:Card

内側のループに引っかかっているようです

まったく逆です。スタンフォードがここでレイアウトした演習は、実際にはループして、スーツごとに1種類のカードを追加しています。ループは問題ではなく、タイプエラーが問題です。

于 2013-03-21T15:16:33.243 に答える
0

私はまったく同じ問題を抱えていました。CodaFi が言ったことは正確であり、私がそれを修正したときに問題を解決しました。私は誤って Deck と言いましたが、あなたは NSObject と言っています。読むようにあなたのものを変更してください:

#import "Deck.h"

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

この 1 つの修正で、両方の問題が修正されました。

ただし、正解は私ではなく CodaFi の功績によるものと考えてください。私もコードを持っていたので、コードを見せただけです。

そして、レクチャー2のスライドを確認したところ、これも書かれていたので、私がだまされたことはわかっています。たくさんのコード。

残りは頑張ってください。

于 2013-03-25T01:42:48.277 に答える