输出参数的Objective-C可为空性 [英] Objective-C nullability for output parameters
问题描述
我正在为类添加可空性说明符,并且我有一些指针到指针的输出参数,例如(NSString**)
,因为该方法返回了多个对象.如何为此指定空性?
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?
对于这些特殊情况,我希望调用者不要传递NULL,但是我不在乎他们是否传递指向nil变量的指针(或者,这是预期的).
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).
起初我尝试了(nonnull NSString**)
,在经过两轮Xcode的建议修复后,以(NSString* _Nonnull *)
结尾,但是第二个*仍然警告:指针缺少可空性类型说明符",没有建议修复.
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.
推荐答案
您有两个指针,因此需要两个可空性说明符.
You have two pointers so you need two nullability specifiers.
- (void)someMethod:(NSString * _Nullable * _Nonnull)out
这意味着您必须传递非null指针,但可能会返回null结果.
This means you must pass in a non-null pointer but you may get back a null result.
这将失败:
[someObject someMethod:nil];
这将起作用:
NSString *result = nil;
[someObject someMethod:&result];
这篇关于输出参数的Objective-C可为空性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!