Created
May 22, 2016 03:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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