Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
james white
78,399 PointsTiming Out an Image using NSTimer, Challenge 2/2
This is the last set of objectives for the last section,
(Retrieving and Viewing Data from Parse.com)
for the "Build a Self-Destructing Message iPhone App" course
Challenge Task 2 of 2
"Remember from the video that whenever we call a selector, it's good practice to be defensive and check if the target responds to the selector before calling it. Add an 'if' statement to do just that!"
Link to challenge:
I got through the first part of the question with the help of Joey's answer from this thread:
https://teamtreehouse.com/forum/code-challenge-timing-out-an-image-using-nstimer
I'm assuming the second part wants me to wrap the added code from the first part of the challenge with some kind of if...else code. maybe?
............................
I found this thread that had a simlar if..else code: https://teamtreehouse.com/forum/ribbit-image-view-controller-issues
Here's the code snippet I found, from Scott Taylor's question,
excerpted from that thread:
if ([self respondsToSelector:@selector(timeout)]) {
[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
}
else {
NSLog(@"Error: selector missing");
}
}
You see it's pretty close!
So, substituting Joey's '[NSTimer...' line of code
with that if..else lines of code gives:
#import "GameViewController.h"
#import "UIAlertView.h"
@implementation GameViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Insert your code here!
if ([self respondsToSelector:@selector(timeout)]){
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
}
else {
NSLog(@"Error: selector missing");
}
}
}
- (void)timeout {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Out of time!"
message:@"Try again!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
}
@end
Unfortunately it gives me this error:
"Bummer! Compilation Error! Hint: Send the message 'respondsToSelector' to the target (don't forget the special selector syntax)."
Any clues (I'm probably just being stupid..or perhaps have some kind of minor syntax mistake).
--however it does look close to the code for this video at time mark 6:26:
So I don't have a clue what's wrong...
2 Answers
james white
78,399 PointsThe code (that complete both challenges):
if ([self respondsToSelector:@selector(timeout)]) {
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeout) userInfo:nil repeats:NO];
}
else {
NSLog(@"Error: selector missing!");
}
Thanks to all those who helped!
Oh wait...the only person that posted in this thread is me!
Never-mind..
That completes all 12 Treehouse tracks for me, though..
james white
78,399 PointsWell, it's been 3 days, but I guess it's been slow on the forum and the Ribbit app is one of the hardest (and longest) of all the courses, but hopefully someone who has completed the course will find this thread eventually.
This is the last objective I need to complete the iOS track.
I heard there's a new Swift course out. They are supposed to be easier than these Objective-c courses, so maybe I'll start in on that while I'm waiting for an answer.