objective-cobjective-c++objective-c-nullability

What does id _Nullable __autoreleasing * _Nonnull mean?


I am confused with the usage of (out id _Nullable *) in the code docs and the error my snippet throws at me.

#import <Foundation/Foundation.h>

void getvalue(char *a, bool *value){
  @autoreleasepool {
    NSURL *url = [[NSURL alloc] initFileURLWithFileSystemRepresentation:a 
                                                            isDirectory:NO 
                                                          relativeToURL:nil];
    [url getResourceValue:value 
                   forKey:NSURLIsDirectoryKey 
                    error:nil];
  }
}

void main (int argc, const char * argv[])
{
  bool value[10];
  char a[] = "a";
  getvalue(a, value + 4);
}

Cannot initialize a parameter of type 'id _Nullable __autoreleasing * _Nonnull' with an lvalue of type 'bool *'

What does _Nullable * _Nonnull mean and what am I doing wrong in the code?

Reference:

getResourceValue:forKey:error:

- (BOOL)getResourceValue:(out id  _Nullable *)value 
                  forKey:(NSURLResourceKey)key 
                   error:(out NSError * _Nullable *)error;

Solution

  • _Nullable * _Nonnull isn't the issue here. The problem is you're passing a pointer to a bool when you need to pass a pointer to a pointer to an object. getResourceValue:forKey:error: returns an object, not a primitive. In this case, an NSNumber.

        NSNumber *result = nil;
        [url getResourceValue:&result
                       forKey:NSURLIsDirectoryKey
                        error:nil];
        *value = [result boolValue];
    

    To the question, id _Nullable * _Nonnull means "a pointer, which must not be null, to an object-pointer, which may be null." This is the standard return-by-reference pattern in ObjC. id is a typedef for a pointer-to-an-object.