3

nullable

I find this syntax rather confusing:

- (void)doSomething:(nullable void (^)(NSArray * _Nullable transactions))successBlock
            failure:(nullable void (^)(NSError * _Nullable error))failureBlock;

...not only because of the duality between nullable and _Nullable, but also because, while having the same intent, they are not interchangeable.

As found in Nullability and Objective-C:

  • use nullable like you would use assertions
  • __nullable is the old name of nullable between Xcode 6.3 and 7.0
  • use _Nullable where you can use const

This makes little sense in the example above, since I have yet to see void defined as const void. Is there and even better version of nullable that could be used interchangeably?


_Nonnull, _Null_unspecified

Same puzzle.

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179

1 Answers1

4

nullable is Objective C language syntax. _Nullable is C language extension, which is inherited by Objective C.

Functions and blocks are part of the C language, so you must use _Nullable in those declarations. Objective C method and property declarations can use either Objective C's nullable or C's _Nullable.


By the way, C function pointer and block syntax is confusing at the best of times. It's often a good idea to use typedefs to simplify complex situations.

typedef void (^SuccessBlock)(NSArray* _Nullable transactions);

- (void)doSomething:(nullable SuccessBlock)successHandler;
Darren
  • 25,520
  • 5
  • 61
  • 71