Problems with makeObjectsPerformSelector inside and outside a class?

Posted by QuakAttak on Stack Overflow See other posts from Stack Overflow or by QuakAttak
Published on 2010-04-27T00:21:38Z Indexed on 2010/04/27 0:23 UTC
Read the original article Hit count: 535

A friend and I are creating a card game for the iPhone, and in these early days of the project, I'm developing a Deck class and a Card class to keep up with the cards. I'm wanting to test the shuffle method of the Deck class, but I am not able to show the values of the cards in the Deck class instance. The Deck class has a NSArray of Card objects that have a method called displayCard that shows the value and suit using console output(printf or NSLog). In order to show what cards are in a Deck instance all at once, I am using this, [deck makeObjectsPerformSelector:@selector(displayCard)], where deck is the NSArray in the Deck class. Inside of the Deck class, nothing is displayed on the console output. But in a test file, it works just fine. Here's the test file that creates its own NSArray:

#import <Foundation/Foundation.h>
#import "card.h"

int main (int argc, char** argv)
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    Card* two = [[Card alloc] initValue:2 withSuit:'d'];
    Card* three = [[Card alloc] initValue:3 withSuit:'h'];
    Card* four = [[Card alloc] initValue:4 withSuit:'c'];
    NSArray* deck = [NSArray arrayWithObjects:two,three,four,nil];

    //Ok, what if we release the objects in the array before they're used?
    //I don't think this will work...
    [two release];
    [three release];
    [four release];
    //Ok, it works... I wonder how...

    //Hmm... how will this work?
    [deck makeObjectsPerformSelector:@selector(displayCard)];
    //Yay! It works fine!

    [pool release];

    return 0;
}

This worked beautifully, so I created an initializer around this idea, creating 52 card objects one at a time and adding them to the NSArray using deck = [deck arrayByAddingObject:newCard]. Is the real problem with how I'm using makeObjectsPerformSelector or something before/after it?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about nsarray