6
votes

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.

1
Keep in mind: Nullability is important for Swift, not for pure Objective-C. - Amin Negm-Awad
Yeah, the reason I'm doing this is I want to access this class from Swift. - Uncommon
There you have it: Poison code in a good language for compatibility in a bad language. :-] - Amin Negm-Awad

1 Answers

11
votes

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];