Disabling nullability warnings in Xcode/Clang

May 27, 2015

The recently added nullability qualifers in Objective C are a welcome addition, but unfortunately adding them to block type definitions can break autocomplete of the block parameter when implementing the method (the empty block that appears when you press Enter). At least it does for me at the time of writing (Xcode 6.3.2), meaning you have to enter the block type manually, which is no fun at all.

As far as I can tell you can’t specify __nullable or __nonnull in the type definition without this happening. Unfortunately, if the file contains one reference to nullability, all other pointers in the file must also be qualified. You get a Block pointer is missing a nullability type specifier otherwise.

Fortunately, you can disable the warning with the -Wnullability-completeness compiler flag. But you probably only want to disable it over a region of a file (presumably the header). You can do this with the #pragma clang diagnostic option.

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef void (^something_t)(NSURL* __nullable);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"

@interface MyClass

/// NB: nullability of object not specified
-(void)methodWithoutNullabilityQualifier:(something_t)object;

@end

#pragma clang diagnostic pop

Now you won’t get the warning about something_t in the @interface definition not specifying nullability (though you should still specify it in the @implementation)