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 > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-05-2010, 02:37 PM   #1 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Exclamation NSInvalidArguementException error

Hi, I am trying to get the timer part on my app working and something with the timer is making it crash.
Code:
- (IBAction)click_button
{
	//NSTimeInterval elapsed_time;
//	NSTimeInterval time_remaining;
	if (!timer_running)
	{
		start_time = [NSDate timeIntervalSinceReferenceDate];
		end_time = start_time + 60;
		timer_running = TRUE;
		NSLog(@"the timer is started");
		display_timer = [NSTimer timerWithTimeInterval: 1 
												target: self 
											  selector: @selector(display_time) 
											  userInfo:nil 
											   repeats: YES];
	}
	else if(timer_running = TRUE)
	{
		timer_running = FALSE;
		[display_timer invalidate];
	
		display_timer = nil;
		NSLog(@"the timer is almost stopped");
		[self performSelector:@selector(display_time)];
	}
}


- (void) display_time
{
	NSLog(@"making sure the timer started");
	time_remaining = end_time  - [NSDate timeIntervalSinceReferenceDate];
	if (time_remaining <= 0)
	{
		//stop the timer, we're done.
		NSLog(@"timer stopped, time is shown in label");
		time_remaining = 0;
		[display_timer invalidate];
		display_timer = nil;
	}
	display_field.text  = [NSString stringWithFormat: @"Time remaining %.1f seconds", time_remaining];
}
then it gives me this error:
Code:
kill
quit

The Debugger has exited with status 0.
[Session started at 2010-06-05 14:35:05 -0500.]
2010-06-05 14:35:09.967 countingtime[20625:20b] the timer is started
2010-06-05 14:35:11.327 countingtime[20625:20b] *** -[NSCFDictionary invalidate]: unrecognized selector sent to instance 0xd37a90
2010-06-05 14:35:11.328 countingtime[20625:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary invalidate]: unrecognized selector sent to instance 0xd37a90'
2010-06-05 14:35:11.330 countingtime[20625:20b] Stack: (
    807902715,
    2421095995,
    808284155,
    807854166,
    807706786,
    9177,
    814709201,
    815110321,
    815119058,
    815114270,
    814813151,
    814722763,
    814748641,
    839148405,
    807687520,
    807683624,
    839142449,
    839142646,
    814752238,
    8308
)
If you know something that might be wrong with this code. It was originally told to me in a different thread i posted where the person said it should work, but it doesn't. If you know how to fix this or know a better way to possibly do this check out the thread "can a timer do this?"
Mjsdabeast is offline   Reply With Quote
Old 06-05-2010, 03:47 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Subtle, very subtle. Look at this line:

Code:
else if(timer_running = TRUE)
you probably want that to be two equals signs for comparison:

Code:
else if(timer_running == TRUE)
I can't see that really causing the problem though; the only way to get to that assignment is if timer_running is already true, so at worst that assignment does nothing. I have a feeling I'm missing something else.

What action causes the crash? You click the button once, and then one second later "boom?"

EDIT: The error you're getting is the one you usually get for invalidating a timer twice, but you are setting your pointer to nil so that should not happen.
__________________

Free Games!

Last edited by smasher; 06-05-2010 at 03:49 PM.
smasher is offline   Reply With Quote
Old 06-05-2010, 04:02 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Found it. You want scheduledTimerWithTimeInterval: instead of timerWithTimeInterval. The method you used returns an autoreleased timer that is not scheduled yet - you need to schedule it yourself before it will fire. So the timer never fired, and the second time you clicked the button it tries to invalidate the (already released) timer and you crash.

This line should work instead:

Code:
display_timer = [NSTimer scheduledTimerWithTimeInterval: 1 
		target: self 
		selector: @selector(display_time) 
		userInfo:nil 
		repeats: YES];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 06-05-2010, 08:05 PM   #4 (permalink)
Registered Member
 
Mjsdabeast's Avatar
 
Join Date: Oct 2009
Location: Illinois
Age: 16
Posts: 94
Mjsdabeast is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Found it. You want scheduledTimerWithTimeInterval: instead of timerWithTimeInterval. The method you used returns an autoreleased timer that is not scheduled yet - you need to schedule it yourself before it will fire. So the timer never fired, and the second time you clicked the button it tries to invalidate the (already released) timer and you crash.

This line should work instead:

Code:
display_timer = [NSTimer scheduledTimerWithTimeInterval: 1 
		target: self 
		selector: @selector(display_time) 
		userInfo:nil 
		repeats: YES];
oh, ok i completely missed that i will change it. thank you
Mjsdabeast is offline   Reply With Quote
Reply

Bookmarks

Tags
crashing, error, nsinvalidarguementexcept, timer

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: 356
12 members and 344 guests
ajay123123, Anwerbl, Arty Tales, ashaman64, baja_yu, ChrisYates, HemiMG, mini998, mottdog, newDev, Objective Zero, oceanlablight
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,878
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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