iosobjective-cobjective-c-nullability

Pointer is missing a nullability type specifier


In Xcode 7 GM I started to get this warning:

Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)

In the following function declaration (NSUserDefaults extension)

- (void)setObject:(nullable id)value
           forKey:(NSString *)defaultName
    objectChanged:(void(^)(NSUserDefaults *userDefaults, id value))changeHandler
    objectRamains:(void(^)(NSUserDefaults *userDefaults, id value))remainHandler;

Screenshot

Why this warning is showing and how should I fix it?


Solution

  • You need to specify nullable also for the handlers/blocks

    - (void)setObject:(nullable id)value
               forKey:(nonnull NSString *)defaultName
        objectChanged:(nullable void(^)(NSUserDefaults *userDefaults, id value))changeHandler
        objectRamains:(nullable void(^)(NSUserDefaults *userDefaults, id value))remainHandler;
    

    Why? It's due to Swift. Swift allows optional parameters (?), which Objective-C does not. This is done as a bridge between the both of them for the Swift compiler to know those parameters are optional. A 'Nonnull' will tell the Swift compiler that the argument is the required. A nullable that it is optional

    For more info read: https://developer.apple.com/swift/blog/?id=25