Skip to content

Instantly share code, notes, and snippets.

@paulrehkugler
Last active December 1, 2015 17:25
Show Gist options
  • Save paulrehkugler/e23ef97e8b0fc1589baf to your computer and use it in GitHub Desktop.
Save paulrehkugler/e23ef97e8b0fc1589baf to your computer and use it in GitHub Desktop.
Nullability and Inheritance
@interface TMObjectThatHasLotsOfDependenciesInjected : NSObject
- (nonnull instancetype)initWithDependency:(TMDependency * __nonnull)dependency
anotherDependency:(TMOtherDependency * __nonnull)anotherDependency NS_DESIGNATED_INITIALIZER;
/// option 1: break inheritance
- (nullable instancetype)init __attribute__((unavailable("Use initWithDependency:anotherDependency: instead")));
@end
@implementation TMObjectThatHasLotsOfDependenciesInjected
- (instancetype)initWithDependency:(TMDependency *)dependency
anotherDependency:(TMOtherDependency *)anotherDependency {
NSParameterAssert(dependency);
NSParameterAssert(anotherDependency);
self = [super init];
if (self) {
// ...
}
return self;
}
/// option 2: break nullability and crash at runtime because of parameter asserts
- (instancetype)init {
return [self initWithDependency:nil anotherDependency:nil]; // passing nil to nonnull arguments
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment