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.
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.
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With