Skip to content

Instantly share code, notes, and snippets.

@matthewreagan
Last active August 29, 2015 13:55
Show Gist options
  • Save matthewreagan/8760631 to your computer and use it in GitHub Desktop.
Save matthewreagan/8760631 to your computer and use it in GitHub Desktop.
-isKindOfClass Macro Shortcut
#define EXPECT(anObj, aClass)\
aClass * anObj##_valid = nil;\
BOOL anObj##_isClass = [anObj isKindOfClass:[aClass class]];\
if (! anObj##_isClass) { NSLog(@"Warning: Invalid class, '%s' is %@, expecting %@", #anObj, [anObj class], [aClass class]); }\
if ( anObj##_isClass )\
{ anObj##_valid = (aClass *) anObj; }\
if ( anObj##_isClass )
// ...replaces this:
id webServiceResponse;
if ([webServiceResponse isKindOfClass:[NSDictionary class]])
{
NSDictionary *typeCast = (NSDictionary *)webServiceResponse;
[typeCast dictionarySpecificMethod];
}
else
{
NSLog(@"Warning: expecting dictionary");
}
// ...with this:
EXPECT(webServiceResponse, NSDictionary)
{
[webServiceResponse_valid dictionarySpecificMethod];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment