Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-30-2010, 08:01 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 2
Anulith is on a distinguished road
Default Problem accessing properties

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?
Anulith is offline   Reply With Quote
Old 03-30-2010, 11:03 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

I think the issue may be with this line of code:
Code:
		_hand = [NSMutableArray array];
I believe this is assigning an autoreleased array instance to your '_hand' member, so when you attempt to access it later, it's no longer really there. Try changing that line to:
Code:
		_hand = [[NSMutableArray array] retain];
// or
		_hand = [[NSMutableArray alloc] init];
// or even
		_hand = [NSMutableArray new];
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 03-30-2010, 01:11 PM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 2
Anulith is on a distinguished road
Default

Thank you! After adding the 'retain' it worked as desired. Why is the NSMutableArray object freed even though the Player object is still alive and holds a reference to the _hand array? I suppose I don't understand the garbage collector of objective c well enough.

Once again, thanks for your insight.
Anulith is offline   Reply With Quote
Reply

Bookmarks

Tags
iphone, nsnotification, nsnotificationcenter, observer, selector

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 462
16 members and 446 guests
7twenty7, AlanFloyd, David-T, iAppDeveloper, imac74, Jaxen66, logan, lovoyl, markuschow, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:08 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0