7

I'm adding nullability specifiers to a class, and I have some pointer-to-pointer output parameters, like (NSString**), because the method returns multiple objects. How do I specify nullability for that?

For these particular cases, I want callers to not pass in NULL, but I don't care if they pass a pointer to a nil variable (or rather, that's expected).

At first I tried (nonnull NSString**), and after a couple of rounds of Xcode's suggested fixes, ended with (NSString* _Nonnull *), but there's still a warning on the second *, "Pointer is missing nullability type specifier", with no suggested fix.

Uncommon
  • 3,323
  • 2
  • 18
  • 36

1 Answers1

14

You have two pointers so you need two nullability specifiers.

- (void)someMethod:(NSString * _Nullable * _Nonnull)out

This means you must pass in a non-null pointer but you may get back a null result.

This will fail:

[someObject someMethod:nil];

This will work:

NSString *result = nil;
[someObject someMethod:&result];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • This somehow isn't working for me. Xcode always complains when I pass in a nil to the someMethod: `warning: null passed to a callee that requires a non-null argument [-Wnonnull] [newClass someMethod:nil]; ^ ~~~ 1 warning generated.` However if I change both the qualifier to _Nullable, it stops complaining (as expected). I still would like to use two distinct qualifiers, `* _Nullable * _Nonnull` but it's not working – Vinod Madigeri Mar 15 '21 at 20:47