Skip to content

Instantly share code, notes, and snippets.

@nsforge
Created December 2, 2013 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsforge/7743616 to your computer and use it in GitHub Desktop.
Save nsforge/7743616 to your computer and use it in GitHub Desktop.
Example of why you shouldn't blindly throw away type information from the compiler when casting to a protocol
- (void)someCallbackWithAViewController:(UIViewController *)viewController
{
if ([viewController conformsToProtocol:@protocol(CrazyViewController)])
{
UIViewController <CrazyViewController> *crazyVC = (UIViewController <CrazyViewController> *)viewController;
if (crazyVC.presentedViewController == nil)
{
[crazyVC showCrazyModalView];
}
else
{
[crazyVC startCrazyViewHideTransition];
}
}
else
{
// do something different
}
}
@nsforge
Copy link
Author

nsforge commented Dec 2, 2013

If you had cast crazyVC to (id <CrazyViewController>), you would get a compiler error on line 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment