Objective-C has a nonnull compiler attribute that can be added to the end of a method or function declaration. Doing this can help document your code and also trigger a compiler warning to the caller of this method if a passed-in argument is null.

For example, in our header, if we have a method that takes two arguments:

@interface TestClass : NSObject

- (void)doStuffWithString1:(NSString *)string1
                   string2:(NSString *)string2;

@end

And we later call this method passing in nil for the second (string2) argument:

TestClass *aTestClass = [[TestClass alloc] init];
[aTestClass doStuffWithString1:@"Hello" string2:nil];

This may not be ideal depending on the implementation of this method. One or more of the arguments may need to be non-null. We could use NSParameterAssert inside the method implementation to enforce this at runtime. But using the nonnull attribute will give off a warning at compile-time, which may be preferred depending on the situation.

So, to fix this, we can add this attribute to our method declaration:

@interface TestClass : NSObject

- (void)doStuffWithString1:(NSString *)string1
                   string2:(NSString *)string2 __attribute__((nonnull(2)));

@end

nonnull(2) means that the second argument (string2) to this method should be non-null. nonnull(1) means the first argument should be non-null. And nonnull(1,2) means that the first and second arguments should be non-null. Declaring nonnull() without any numbers will require that all arguments should be non-null.

So, for our example, calling [aTestClass doStuffWithString1:@"Hello" string2:nil]; with our nonnull(2) declaration will trigger a compile-time warning from the compiler of: Null passed to a callee which requires a non-null argument.