Search code examples
objective-cannotationsdeclarationmethod-declaration

Declaration of a method with __attribute__((nonnull(2)))


What does this __attribute__((nonnull(2))) mean in the following method declaration?

 - (void)requestShareWithObjectId:(NSString *)object 
                       completion:(void (^)(NSString *likes, NSString *reposts))completion __attribute__((nonnull(2)));

Solution

  • It denotes that the Second parameter should not be a null pointer.

    __attribute__((nonnull))

    This function attribute specifies function parameters that are not supposed to be null pointers. This enables the compiler to generate a warning on encountering such a parameter.

    Syntax

    __attribute__((nonnull(arg-index, ...)))

    Where arg-index, ... denotes the argument index list.

    If no argument index list is specified, all pointer arguments are marked as nonnull.

    References

    1. NSHipster - __attribute__
    2. ARM
    3. Keil