Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C nullability for output parameters

Tags:

objective-c

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.

like image 906
Uncommon Avatar asked Jun 03 '16 15:06

Uncommon


People also ask

What does nullable mean in Objective-C?

nonnull : the value won't be nil. It bridges to a Swift regular reference. nullable : the value can be nil. It bridges to a Swift optional. null_resettable : the value can never be nil when read, but you can set it to nil to reset it.

What is ID in Obj C?

id is the generic object pointer, an Objective-C type representing "any object". An instance of any Objective-C class can be stored in an id variable.


1 Answers

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];
like image 176
rmaddy Avatar answered Oct 19 '22 23:10

rmaddy