iphone - Problems with makeObjectsPerformSelector inside and outside a class? -
a friend , creating card game iphone, , in these days of project, i'm developing deck class , card class keep cards. i'm wanting test shuffle method of deck class, not able show values of cards in deck class instance. deck class has nsarray of card objects have method called displaycard shows value , suit using console output(printf or nslog). in order show cards in deck instance @ once, using this, [deck makeobjectsperformselector:@selector(displaycard)]
, deck nsarray in deck class. inside of deck class, nothing displayed on console output. in test file, works fine. here's test file creates own nsarray:
#import <foundation/foundation.h> #import "card.h" int main (int argc, char** argv) { nsautoreleasepool* pool = [[nsautoreleasepool alloc] init]; card* 2 = [[card alloc] initvalue:2 withsuit:'d']; card* 3 = [[card alloc] initvalue:3 withsuit:'h']; card* 4 = [[card alloc] initvalue:4 withsuit:'c']; nsarray* deck = [nsarray arraywithobjects:two,three,four,nil]; //ok, if release objects in array before they're used? //i don't think work... [two release]; [three release]; [four release]; //ok, works... wonder how... //hmm... how work? [deck makeobjectsperformselector:@selector(displaycard)]; //yay! works fine! [pool release]; return 0; }
this worked beautifully, created initializer around idea, creating 52 card objects 1 @ time , adding them nsarray using deck = [deck arraybyaddingobject:newcard]
. real problem how i'm using makeobjectsperformselector or before/after it?
when add objects array, increments retaincount one. why releasing doesn't dealloc object. array created in autoreleased state. when release pool, releases array , objects. must understand release not equal free. decrements retaincount. when retaincount goes 0 freed. outside of function, must somehow retain object.
Comments
Post a Comment