Hello all and thanks in advance for taking the time to help me out. I'm pretty new to objective-c and am having trouble with notification handlers. I have a object that registers itself as an observer of a notification. The notification is fired and I can access the object that fired the notification from my observer function just fine but I can't access any properties of self. Everytime I try to I get a bad selector or bad access error.
I have this same problem whether trying to access properties from the touch event handler or my own custom event handler. Below is my init and observer code.
Code:
- (id) initWithPlayerType : (PlayerType) type
{
if((self = [super init]))
{
//initialize
_playerType = type;
_hand = [NSMutableArray array];
switch(_playerType)
{
case PLAYER:
_partner = PARTNER;
isTouchEnabled = YES;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(cardPlayed:)
name:@"cardPlayed"
object:nil ];
break;
case OPPONENT_1:
_partner = OPPONENT_2;
break;
case PARTNER:
_partner = PLAYER;
break;
case OPPONENT_2:
_partner = OPPONENT_1;
break;
default:
break;
}
}
return self;
}
- (void) cardPlayed: (NSNotification *) notification
{
NSLog(@"Card Played:");
[[notification object] logValue];
//the next line is where the exception is always thrown
int handCount = [_hand count];
for(int i=0; i<handCount; i++)
{
if([[_hand objectAtIndex:i] compare:[notification object]] == 0)
{
[_hand removeObjectAtIndex:i];
break;
}
}
//this is my original code that was giving me problems so
//I tried to do the above
//[_hand removeObject:[notification object]];
[self adjustCardPositions];
}
Am I missing something about how all this works? Do I not have access to the properties of the observer object because it goes out of scope somehow?