Skip to content

Instantly share code, notes, and snippets.

@sodastsai
Last active November 8, 2016 21:27
Show Gist options
  • Save sodastsai/b4c2147b7b1aef467b5a548570a0036d to your computer and use it in GitHub Desktop.
Save sodastsai/b4c2147b7b1aef467b5a548570a0036d to your computer and use it in GitHub Desktop.
ObjC Generic
@interface BaseViewModel : NSObject
- (void)baseMethod;
@end
@implementation BaseViewModel
- (void)baseMethod {}
@end
// ---------------------------------------------------------------------------------------------------------------------
@interface BaseViewController<__covariant ViewModelType: BaseViewModel *> : UIViewController
@property (nonatomic, strong) ViewModelType viewModel;
@end
@implementation BaseViewController
- (void)ha {
[self.viewModel baseMethod];
}
@end
// =====================================================================================================================
@interface MyViewModel : BaseViewModel
- (void)myMethod;
@end
@implementation MyViewModel
- (void)myMethod {}
@end
// ---------------------------------------------------------------------------------------------------------------------
@interface MyViewController : BaseViewController<MyViewModel *>
@end
@implementation MyViewController
- (void)yo {
[self.viewModel baseMethod];
[self.viewModel myMethod];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment