View BadUnpack.m
static void badPrintExample(Example * _Nonnull example) {
[example switchFoo:^(ExampleBar * _Nonnull bar_) {
// ^
// error: incompatible block pointer types sending 'void (^)(ExampleBar * _Nonnull __strong)' to parameter of type 'void (^ _Nonnull)(ExampleFoo * _Nonnull __strong)'
NSLog(@"Bar: %@, %@", @(bar_.b), @(bar_.c));
}
bar:^(ExampleFoo * _Nonnull foo_) {
// ^
// error: incompatible block pointer types sending 'void (^)(ExampleFoo * _Nonnull __strong)' to parameter of type 'void (^ _Nonnull)(ExampleBar * _Nonnull __strong)'
NSLog(@"Foo: %@, %@", foo_.f, foo_.g);
View BadInstantiation.m
- (void)testWrongValues {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithB:2 c:3];
// ^
// error: no visible @interface for 'ExampleFoo' declares the selector 'initWithB:c:'
Example * _Nonnull barExample = [[ExampleBar alloc] initWithF:@"foo" g:@"goo"];
// ^
// error: no visible @interface for 'ExampleBar' declares the selector 'initWithF:g:'
}
View MissingReturn.m
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
- (void)missingReturn {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
NSNumber * _Nonnull number =;
[ExampleSwitcher<NSNumber *> nonnullExampleFrom:fooExample
switchFoo:^(ExampleFoo * _Nonnull foo_) {
return @(foo_.f.length + foo_.g.length);
}
View MissingAssignment.m
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
- (void)missingAssignment {
Example * _Nonnull fooExample = [[ExampleFoo alloc] initWithF:@"foo"
g:@"goo"];
NSNumber * _Nonnull __block number;
[fooExample switchFoo:^(ExampleFoo * _Nonnull foo_) {
number = @(foo_.f.length + foo_.g.length);
}
bar:^(ExampleBar * _Nonnull bar_) {
View ExampleTestsTestTransformExample.m
#import "Example.h" // https://gist.github.com/hborders/e00d7ff965ca8f8df8f7e762a2269efb
#import "ExampleSwitcher.h" // https://gist.github.com/hborders/f378b4e0d0f1d74ff7c8149cb04c6268
#import "ExampleFoo.h" // https://gist.github.com/hborders/094f05d932b3d7a1e389184ba525b0c3
#import "ExampleBar.h" // https://gist.github.com/hborders/f1c231a89de11157adf46ee85880733a
static NSNumber * _Nonnull transformExample(Example * _Nonnull example) {
return [ExampleSwitcher<NSNumber *> nonnullValueFrom:example
switchFoo:^NSNumber * _Nonnull (ExampleFoo * _Nonnull foo_) {
return @(foo_.f.length + foo_.g.length);
}
View ExampleBar.h
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
@interface ExampleBar : Example
@property (nonatomic, readonly) NSInteger b;
@property (nonatomic, readonly) NSInteger c;
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
- (instancetype _Nonnull)initWithB:(NSInteger)b
c:(NSInteger)c NS_DESIGNATED_INITIALIZER;
View ExampleFoo.h
#import "Example.h" // https://gist.github.com/hborders/2af9b39e27b62ef9e68c65085126fe4a
@interface ExampleFoo : Example
@property (nonatomic, readonly) NSString * _Nonnull f;
@property (nonatomic, readonly) NSString * _Nonnull g;
+ (instancetype _Nonnull)new NS_UNAVAILABLE;
- (instancetype _Nonnull)init NS_UNAVAILABLE;
- (instancetype _Nonnull)initWithF:(NSString * _Nonnull)f
g:(NSString * _Nonnull)g NS_DESIGNATED_INITIALIZER;
View ExampleBar.m
#import "ExampleBar.h" // https://gist.github.com/hborders/f1c231a89de11157adf46ee85880733a
#import "ExamplePrivate.h" // https://gist.github.com/hborders/12fcbe34cf4de68c2317bad92a8e223d
@implementation ExampleBar
- (instancetype _Nonnull)initPrivate {
NSLog(@"Unavailable");
abort();
}
- (instancetype _Nonnull)initWithB:(NSInteger)b
c:(NSInteger)c {
View ExampleFoo.m
#import "ExampleFoo.h" // https://gist.github.com/hborders/094f05d932b3d7a1e389184ba525b0c3
#import "ExamplePrivate.h" // https://gist.github.com/hborders/12fcbe34cf4de68c2317bad92a8e223d
@implementation ExampleFoo
- (instancetype _Nonnull)initPrivate {
NSLog(@"Unavailable");
abort();
}
- (instancetype _Nonnull)initWithF:(NSString * _Nonnull)f
g:(NSString * _Nonnull)g {
View ExampleSwitcher.m
#import "ExampleSwitcher.h" // https://gist.github.com/hborders/f378b4e0d0f1d74ff7c8149cb04c6268
@implementation ExampleSwitcher
+ (NSObject * _Nonnull)nonnullValueFrom:(Example * _Nonnull)example
switchFoo:(NSObject * _Nonnull (^ _Nonnull)(ExampleFoo * _Nonnull foo_))fooBlock
bar:(NSObject * _Nonnull (^ _Nonnull)(ExampleBar * _Nonnull bar_))barBlock {
NSParameterAssert(example);
NSParameterAssert(fooBlock);
NSParameterAssert(barBlock);