Last active
December 1, 2015 17:25
-
-
Save paulrehkugler/e23ef97e8b0fc1589baf to your computer and use it in GitHub Desktop.
Nullability and Inheritance
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
@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 |
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
@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