Skip to content

Instantly share code, notes, and snippets.

@iamleeg
Created February 3, 2012 18:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamleeg/1731505 to your computer and use it in GitHub Desktop.
Save iamleeg/1731505 to your computer and use it in GitHub Desktop.
Causing methods generated by AppCode to throw exceptions

Go to "File Templates" in the Preferences window, then under the 'code' tab choose "OC implemented method body" and paste the template method in there. This then causes AppCode to generate methods like this:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
    [[NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unimplemented method" userInfo:nil] raise];
    return 0;
    //To change the template use AppCode | Preferences | File Templates.
    
}

Another approach would be to use the usual forwarding approach by putting this as the first line:

[self doesNotRecognizeSelector: _cmd];

I decided not to do that as it makes the behaviour of unimplemented methods indistinguishable from generated methods. The reason I want them to throw an exception is that even if the generated return value is sufficient to get a test to pass, I want to be reminded that I haven't yet thought about what this method should do :-).

[[NSException exceptionWithName: NSInternalInconsistencyException reason: @"Unimplemented method" userInfo: nil] raise];
#if ($DEFAULT_RETURN_VALUE == "result")
$RETURN_TYPE result;
return result;
#elseif ($RETURN_TYPE != "void")
return $DEFAULT_RETURN_VALUE;
#end //To change the template use AppCode | Preferences | File Templates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment