-1

Forewarning: I'm going through the Itunes Stanford course for IOS, and I'm a complete noob with this language.

I have this function in my "Deck.m" file.

- (id)init {
 self = [super init];

 if (self) {
     for (NSString *suit in [Card validSuits]) {
         for (NSUInteger rank = 0; rank < [[Card validRanks] count]; rank++) {
             Card *card = [[Card alloc] init];
             card.contents = [[[Card validRanks] objectAtIndex:rank] stringByAppendingString:suit];
             [self.cards addObject:card];
             NSLog(@"%@", [self.cards count]);
         }
     }
 }

 return self;
}

I also have this function in my "Card.h" file (which deck is composed of )

@interface Deck : NSObject

@property (nonatomic, strong) NSMutableArray *cards;

- (Card *)drawRandomCard;

@end

The problem I'm having is that when I print the count when running the program, it's always 0. When i try to print the card, they look fine. When I try to print the card by indexing the array, they're all (null).

What's wrong?

volk
  • 1,196
  • 1
  • 12
  • 31

2 Answers2

4

self.cards needs to be initialized, otherwise you're calling -[nil addObject:].

Wevah
  • 28,182
  • 7
  • 83
  • 72
  • damn it. the exact problem that the professor of the course clearly stated to watch out for. thanks a ton – volk Jun 05 '13 at 03:41
1

I dont think you have initialize the array :

inside init initialize it :

cards=[[NSMutableArray alloc]init];
V-Xtreme
  • 7,230
  • 9
  • 39
  • 79