views:

118

answers:

2

I have a lot of experience with java and c++ development, so classes and abstracting data is pretty easy for me. I only started objective C a short time ago, and i was mostly working with in class globals, and everything was progressing smoothly. I just decided to abstract a large portion of my code in an effort to make it less spaghetti code, and i hit a problem that i haven't been able to overcome since Saturday.

Outline:

I am making a playing card game. I recently abstracted the card data into a class containing 3 variables; (int)i_cardValue, (int)i_cardSuit, and (UIImage *)uii_cardImage. I create a card by calling

-(PlayingCard *)initWithValue:(int)i_initValue suit:(int)i_initSuit image:(UIIMage*)uii_initImage {

     [self setCardValue:i_initValue];

     [self setCardSuit:i_initSuit];

     [self setCardImage:i_initImage];

     return self;

}

I also abstracted the engine part (basically deck functions, such as shuffle and deck managment). The deck is a mutable array, and i have an init deck function.

NSMutableArray *nsma_deck;
-(void)initDeck {
     nsma_deck = [[NSMutableArray alloc] initWithCapacity:52];
}

Now, in my view controller, i make a call to initDeck, then i add items to the deck with a line such as

[pce_Engine initDeck];
[[pce_Engine nsma_deck] addObject:[[PlayingCard alloc] initWithValue:13 suit:2 image:[UIImage imageNamed:@"2C.png"]]];

pce_Engine is a variable of type PlayingCardEngine. I put NSLog lines into the initDeck, and they do not display in console when i run my program. Are they not being called, so the array isnt getting alloced, and thus i cant add stuff to the array? I thought i did this all right, but either im doing it wrong or im missing something, because adding it to the view does not get it to display. Using an NSLog, the deck == nil, so i guess the problem is that i am not accessing the deck correctly and thus not adding values and only have empty variables everywhere :( . I tried looking at a lot of tutorials for objective c, but I haven't even found one that abstracts as far as I am going, so I haven't been able to find my problem.

Can anyone point me in the right direction?

+3  A: 

I presume your card class is a subclass of NSObject in which case you should implement designated initializer (init) which calls [super init] and does generic setup. And in your initWith... method you should call [self init] and then do the assignment.

Here's a good post about cocoa/objective-c initializers: http://mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html

Same goes with class for deck management. Hope this helps to find your problem.

Eimantas
thx for the link, i know the inits are incorrect/not really inits, i was stressing over how this didnt work and i did have [super init] and [self init]s, and i ended up changing so much code in a effort to get passed the problem it came out looking like this >.<.
ColdLogic
A: 

First of all, I agree with Eimantas that your init method is missing a call to [super init] (the value of which you should be returning, read up on what a normal init method looks like).

But as for the init_deck - just what method in your view controller do you have those two lines? I'm thinking it's not being called more based on where the calling code is than anything else (although pce_Engine may also be nil when that code is called in which case it would not work at all).

Kendall Helmstetter Gelner
omg, thank you for catching the pce_Engine wasn't initialized. i added that one line of code and everything worked (^.^).To answer your question, the init_deck was in the viewDidLoad method, and since i was never creating a new engine, it wasnt ever calling the inits.
ColdLogic