Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created May 22, 2016 03:17
Show Gist options
  • Save wh1pch81n/b82e953f70c96f1b8e676123416c8207 to your computer and use it in GitHub Desktop.
Save wh1pch81n/b82e953f70c96f1b8e676123416c8207 to your computer and use it in GitHub Desktop.
Objective c generics for something that isn't a collection type. Here is an example of producing a view controller from storyboard where you are returned the correct type thanks to generics.
// Making a class since we can not add generics to UIStoryboard nor can we make a category for it.
@interface UIStoryboardHelper <__covariant T:UIViewController *> : UIStoryboard
- (T)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard;
@end
@implementation UIStoryboardHelper
- (UIViewController *)vcFromIdentifier:(NSString *)identifier fromStoryboard:(UIStoryboard *)storyboard {
return [storyboard instantiateViewControllerWithIdentifier:identifier];
}
@end
//usage
GreenViewController *vc = [[UIStoryboardHelper <GreenViewController *> new] vcFromIdentifier:NSStringFromClass([GreenViewController class]) fromStoryboard:self.storyboard];
[self presentViewController:vc animated:YES completion:nil];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment