What causes "Missed Method" in this code? - iphone

Apologies ahead of time if this is a completely off-the-mark question, or if I'm not including enough information - I'm very new to iOS development (and Objective-C), and have a habit of jumping into the deep end...
I'm having trouble understanding the "callDelegate" code in GameCenterManager.m that's in the GKTapper Sample Code and also provided in this tuts+ tutorial: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-game-center-achievements-and-leaderboards-part-2/
This is the code:
- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
assert([NSThread isMainThread]);
if([delegate respondsToSelector: selector])
{
if(arg != NULL)
{
[delegate performSelector: selector withObject: arg withObject: err];
}
else
{
[delegate performSelector: selector withObject: err];
}
}
else
{
NSLog(#"Missed Method");
}
}
My app always logs that "Missed Method" line, but I'm not sure what this callDelegate code is actually doing (so I can't fix it). I figure the best way forward is to learn what this is actually doing, and get better output than 'Missed Method'...
One caveat is that my app is currently using Game Center in sandbox mode, since I'm still developing it. This 'Missed Method' line might be expected in this situation - I'm not sure of that, either.
Would anybody be able to translate this code into paragraph form? I'm particularly unsure about the '[delegate respondsToSelector: selector]' piece.
Alternatively, would anybody be able to rewrite the NSLog line so that it outputs more/relevant detail about the problem? I tried this in the hopes of seeing which selector is not going through 'respondsToSelector' properly, but it didn't seem to work:
NSLog(#"Missed Method, %#", selector);

T'he best way to see exactly what is happening is putting a breakpoint at the beginning of callDelegate and then debugging your program, instead of simply running it. You can debug by pressing cmd-y.
Doing like this, each time your program enters the callDelegate function, it stops and the debugger window pops up. There you will be able to inspect where the call came from and what the parameters are.
As to a plain description of this function I would say that it is an helper function that wraps a call to a selector by preceding it with a check of existence of that selector. Instead of checking each time and the performing the selector, you call the helper function that will do both things for you.
So, the reason you always see the log line is that the function you would like to call is not there. By using the debugger you will be able to see which function it is, which class is missing it, and who attempted the operation.

Some comments requested further details about what I commented out to resolve this issue. I didn't think this was good to add as an edit to the question, since it specifically goes over the resolution. It's been a while since I've worked on this project, so it's not all at the top of my head & I'm not sure I've done everything correctly ... I'll do my best to explain it, though.
In the file GameCenterManager.h, it looks like authenticateLocalUser is being initialized:
- (void) authenticateLocalUser;
In the file App_NameViewController.m, viewDidLoad is checking to see if Game Center is available:
self.currentLeaderBoard = kLeaderboardID;
if ([GameCenterManager isGameCenterAvailable]) {
self.gameCenterManager = [[[GameCenterManager alloc] init] autorelease];
[self.gameCenterManager setDelegate:self];
[self.gameCenterManager authenticateLocalUser];
} else {
// The current device does not support Game Center.
}
In the file GameCenterManager.m
- (void) authenticateLocalUser
{
if([GKLocalPlayer localPlayer].authenticated == NO)
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
{
// report any unreported scores or achievements
[self retrieveScoresFromDevice];
[self retrieveAchievementsFromDevice];
//[self callDelegateOnMainThread: #selector(processGameCenterAuth:) withArg: NULL error: error];
}];
}
}
The line that I commented out, which moved me past the Missed Method error, was:
[self callDelegateOnMainThread: #selector(processGameCenterAuth:) withArg: NULL error: error];
I hope this helps!

Add this code to Game_CenterViewController.m you will see the error
- (void)processGameCenterAuth:(NSError *)error{
NSLog(#"error %#", error);
}

Related

Implement game center, and "missed method" error [duplicate]

Apologies ahead of time if this is a completely off-the-mark question, or if I'm not including enough information - I'm very new to iOS development (and Objective-C), and have a habit of jumping into the deep end...
I'm having trouble understanding the "callDelegate" code in GameCenterManager.m that's in the GKTapper Sample Code and also provided in this tuts+ tutorial: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-game-center-achievements-and-leaderboards-part-2/
This is the code:
- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
assert([NSThread isMainThread]);
if([delegate respondsToSelector: selector])
{
if(arg != NULL)
{
[delegate performSelector: selector withObject: arg withObject: err];
}
else
{
[delegate performSelector: selector withObject: err];
}
}
else
{
NSLog(#"Missed Method");
}
}
My app always logs that "Missed Method" line, but I'm not sure what this callDelegate code is actually doing (so I can't fix it). I figure the best way forward is to learn what this is actually doing, and get better output than 'Missed Method'...
One caveat is that my app is currently using Game Center in sandbox mode, since I'm still developing it. This 'Missed Method' line might be expected in this situation - I'm not sure of that, either.
Would anybody be able to translate this code into paragraph form? I'm particularly unsure about the '[delegate respondsToSelector: selector]' piece.
Alternatively, would anybody be able to rewrite the NSLog line so that it outputs more/relevant detail about the problem? I tried this in the hopes of seeing which selector is not going through 'respondsToSelector' properly, but it didn't seem to work:
NSLog(#"Missed Method, %#", selector);
T'he best way to see exactly what is happening is putting a breakpoint at the beginning of callDelegate and then debugging your program, instead of simply running it. You can debug by pressing cmd-y.
Doing like this, each time your program enters the callDelegate function, it stops and the debugger window pops up. There you will be able to inspect where the call came from and what the parameters are.
As to a plain description of this function I would say that it is an helper function that wraps a call to a selector by preceding it with a check of existence of that selector. Instead of checking each time and the performing the selector, you call the helper function that will do both things for you.
So, the reason you always see the log line is that the function you would like to call is not there. By using the debugger you will be able to see which function it is, which class is missing it, and who attempted the operation.
Some comments requested further details about what I commented out to resolve this issue. I didn't think this was good to add as an edit to the question, since it specifically goes over the resolution. It's been a while since I've worked on this project, so it's not all at the top of my head & I'm not sure I've done everything correctly ... I'll do my best to explain it, though.
In the file GameCenterManager.h, it looks like authenticateLocalUser is being initialized:
- (void) authenticateLocalUser;
In the file App_NameViewController.m, viewDidLoad is checking to see if Game Center is available:
self.currentLeaderBoard = kLeaderboardID;
if ([GameCenterManager isGameCenterAvailable]) {
self.gameCenterManager = [[[GameCenterManager alloc] init] autorelease];
[self.gameCenterManager setDelegate:self];
[self.gameCenterManager authenticateLocalUser];
} else {
// The current device does not support Game Center.
}
In the file GameCenterManager.m
- (void) authenticateLocalUser
{
if([GKLocalPlayer localPlayer].authenticated == NO)
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error)
{
// report any unreported scores or achievements
[self retrieveScoresFromDevice];
[self retrieveAchievementsFromDevice];
//[self callDelegateOnMainThread: #selector(processGameCenterAuth:) withArg: NULL error: error];
}];
}
}
The line that I commented out, which moved me past the Missed Method error, was:
[self callDelegateOnMainThread: #selector(processGameCenterAuth:) withArg: NULL error: error];
I hope this helps!
Add this code to Game_CenterViewController.m you will see the error
- (void)processGameCenterAuth:(NSError *)error{
NSLog(#"error %#", error);
}

Block inside block = EXC_BAD_ACCESS

I have a singleton class the handle all the Game Center logic:
typedef void (^GameCenterCallbackFinishUpdating)();
- (void)getAllMatches:(GameCenterCallbackFinishUpdating)onComplete
{
[GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error)
{
//Do stuff here...
onComplete();
}];
}
From another viewController I use:
[[GameCenterHelper sharedHelper] getAllMatches:^{
[self.myTableView reloadData];
}];
It works great when I'm in the app, but once I close the app (background) and then start it up again, I get:
onComplete(); ---- Thread 1: EXC_BAD_ACCESS (code=2, address=0xc)
What am I doing wrong here?
some background info: the blocks are objects and if any block is nil and you try to call them, it crashes the application.
somewhere and somehow the block onComplete becomes nil before you call it. the following if (...) statement helps you to prevent to call a nil pointer, so the application won't crash.
if (onComplete) onComplete();
Thanks to #holex and #Paul.s for explaining it well.
I had the similar situation where I was sending block as method parameter(completionHandler).
- (void)callX:(NSString *)xyz withCompletionHandler:(void (^)(NSString *response))completion
{
completion(something);
}
And there are two situations either I am using this block like:
[MyClass sharedInstance] callX:#"abc" withCompletionHandler:^(NSString *response) {
if (response) {
//do something
}
}];
or this block could be nil as method parameter:
[MyClass sharedInstance] callX:#"abc" withCompletionHandler:nil];
In second case when block was being passed nil as method parameter this caused EXC_BAD_ACCESS on completion(). So as #holex states that the blocks are objects and if any block is nil and you try to call them, it crashes the application.
A single if saves lot of my time
- (void)callX:(NSString *)xyz withCompletionHandler:(void (^)(NSString *response))completion
{
if (completion)
completion(something);
}
P.S: this explanation only for NERDS like me. | ' L ' |

cocos2d schedule selector error

i have an error when scheduling a method. (to display how many star you get according to your score. i have addStar0 addStar1 addStar2 addStar3 methods)
[self schedule:#selector(addStar0) interval:0.2f];
and the methods are:
-(void) addstar0 {
[self unschedule:_cmd];
if (star > starProgress) {
starProgress++;
[self schedule:#selector(addStar1) interval:0.5f];
}
else {
[self schedule:#selector(displayResult) interval:0.5f];
}
}
error message:
Signature not found for selector - does it have the following form? -(void) name: (ccTime) dt
I've tried changing all my methods into addStar1: (ccTime) delta and schedule: #selector(addStar:) interval: 0.2f, but still the same. (actually i've used both ways to schedule( with/without parameter), and both of them worked well in my last project. )
also, one weird thing: sometimes i can't use [self schedule: something], but [CCScheduler sharedScheduler] schedule: something] works fine. I guess it may be project settings or so?
Please answer it with code. thank you.
You are using 'addStar0' for selector
[self schedule:#selector(addStar0) interval:0.2f];
then
-(void) addstar0 {
should be
-(void) addStar0 {
I think your error is actually on [self unschedule:_cmd];
Where is _cmd defined? Try commenting out that line and running it again.
put [self unschedule:_cmd]; after the else statement

UIAlertView clickedButtonAtIndex EXC_BAD_ACCESS

I currently have a UIAlertView being shown with two option buttons. When the user presses one of the buttons, I would like a method (in the same object) to be called that would then retrieve a file from the web.
I can call the method fine and can call NSLog(), but as soon as I come to use any object variables, I get an EXC_BAD_ACCESS error.
My first thought was it could be a threading issue, so thought calling NSNotificationCenter might solve it, but that too ends in the same error.
Below is the code I've got at the moment. I have tried a few different things (some are commented out) to no avail. The 'dbURL' object is a property of the class. (Edit: below code is not complete)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//If database update
[[NSNotificationCenter defaultCenter] postNotificationName:#"newdb" object:self]; //EXC_BAD_ACCESS happens here
if ([alertView.title isEqualToString: #"Database Update"]){
switch (buttonIndex) {
case 0:
//[self getNewDatabase];
//[self performSelectorOnMainThread:#selector(getNewDatabase) withObject:nil waitUntilDone:NO];
//[NSThread detachNewThreadSelector:#selector(getNewDatabase) toTarget:self withObject:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"newdb" object:self];
break;
// Get a new database file from the server
- (void)getNewDatabase{
NSLog(#"in database: %#", dbURL);
}
Thanks in advance!
The problem was an object I was calling as not being retained properly and therefore the reference was lost, resulting in an EXC_BAD_ACCESS error
Thanks to Nick Weaver. Using the NSZombieEnabled argument in the build options helped to identify the rogue reference.
You can't compare NSString with == . You need to use isEqualToString:
[alertView.title isEqualToString:#"Database Update"]

Delegate functions not being called

Long time lurker, first time poster.
I'm making a ServerConnection module to make it a whole lot modular and easier but am having trouble getting the delegate called. I've seen a few more questions like this but none of the answers fixed my problem.
ServerConnection is set up as a protocol. So a ServerConnection object is created in Login.m which makes the call to the server and then add delegate methods in Login to handle if there's an error or if it's done, these are called by ServerConnection like below.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if( [self.delegate respondsToSelector:#selector(connectionDidFinish:)]) {
NSLog(#"DOES RESPOND");
[self.delegate connectionDidFinish:self];
} else {
NSLog(#"DOES NOT RESPOND");
}
self.connection = nil;
self.receivedData = nil;
}
It always "does not respond". I've tried the CFRunLoop trick (below) but it still doesn't work.
- (IBAction)processLogin:(id)sender {
// Hide the keyboard
[sender resignFirstResponder];
// Start new thread
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Acutally call the server
[self authenticate];
// Prevent the thread from exploding before we've got the data
CFRunLoopRun();
// End thread
[pool release];
}
I copied the Apple URLCache demo pretty heavily and have compared them both many times but can't find any discrepancies.
Any help would be greatly appreciated.
Here are the questions to ask:
Does your delegate respond to connectionDidFinishLoading:?
Does the signature match, i.e. it takes another object?
Is the delegate set at all or is it nil? (Check this in that very method)
If any of those are "NO", you will see "doesn't respond"... and all equally likely to happen in your application, but all are easy to figure out.