Uncommon Uncommon - 2 years ago 111
Objective-C Question

Objective-C nullability for output parameters

I'm adding nullability specifiers to a class, and I have some pointer-to-pointer output parameters, like

, 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.

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download