Skip to content

Instantly share code, notes, and snippets.

@memorysaver
Forked from sodastsai/objc.m
Last active November 9, 2016 00:17
Show Gist options
  • Save memorysaver/536f28d1f6785200db5ccdb913d319d4 to your computer and use it in GitHub Desktop.
Save memorysaver/536f28d1f6785200db5ccdb913d319d4 to your computer and use it in GitHub Desktop.
ObjectiveC Lightweight 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