James Singley James Singley - 2 months ago 7
Objective-C Question

I seem to be stuck in an inner loop

I am attempting to complete assignment 2 from the current objective c course available on itunes U. As near as I can tell, I had entered in all the code correctly from the slides but I still get an error during runtime. I added a couple of NSLog outputs around the only error flag I have to see exactly what was going on. The app compiles fine but hits the error at runtime.

It appears the loop is running correctly until the last time it runs. I'm not sure how to break it out.

The generated log follows.

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


Error is yellow triangle that reads: Incompatible pointer types sending '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


Added the following:

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

Answer

Alright, let's fix these one by one:

Error is yellow triangle that reads: Incompatible pointer types sending 'PlayingCard *_strong parameter of type 'Card*'

This is a type error. An object of type Card and an object of type PlayingCard are incompatible, even though they both (or so I assume) descend from NSObject. Type inheritance means that when you have a base class T, and two subclasses of T (S and C), that S and C are incompatible types because they cannot safely be resolved to each other. Objective-C allows you to forgo this type safety, so the way to fix this is either to refactor your method to accept any object, id, or to make PlayingCard a subclass of Card, rather than just importing its definition. Inheritance is dictated by the superclass on the right-hand side of the colon, not by import statements.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PlayingCard isFaceUp]: unrecognized selector sent to instance 0x8670610'

This, again, stems from a type incompatibility. I can assume that the Card class is the only one that implements -isFaceUp:, not PlayingCard. If so, just make PlayingCard inherit from Card. If not, implement -isFaceUp: or forward it to an internal instance of Card.

Seems to be stuck in an inner loop

Quite the contrary. The exercise Stanford layed out here is actually looping through and adding one type of card for every suit. The loop isn't the problem, your type errors are.

Comments