NS_ASSUME_NONNULL_END 之后的可空性注释警告

Nic*_*kCE 5 xcode objective-c ios swift objective-c-nullability

我正在努力向头文件的一部分添加一些可空性注释,并且我希望块假定为非空,但之后几乎每个方法都发出警告,说我需要添加可空性注释。

下面的示例代码:

NS_ASSUME_NONNULL_BEGIN
- (void)testMethodWithParameter:(NSString *)par otherParameter:(NSString *)otherPar;
NS_ASSUME_NONNULL_END

- (void)methodThatShouldntNeedAnnotationWithText:(NSString *)txt;
//Warning: Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)
Run Code Online (Sandbox Code Playgroud)

我是否错误地理解了这些宏?我认为它会包含据说在 BEGIN/END 块内审计的部分,但外面的一切仍然是_Null_unspecified

这是按预期工作的,还是我必须做些什么才能按照我的想法进行工作?

Fan*_*ini 3

NS_ASSUME_NONNULL_BEGIN使任何参数被假定为非空变量,因此:

NS_ASSUME_NONNULL_BEGIN
- (void)testMethodWithParameter:(NSString *)par otherParameter:(NSString *)otherPar;
NS_ASSUME_NONNULL_END
Run Code Online (Sandbox Code Playgroud)

转换为:

- (void)testMethodWithParameter:(nonnull NSString *)par otherParameter:(nonnull NSString *)otherPar;
Run Code Online (Sandbox Code Playgroud)

因此,缺少NS_ASSUME_NONNULL_BEGIN并不意味着每个参数都被假定为nullableunspecified,您仍然需要定义它。因此,对于另一种方法,您必须指定您想要的内容,如果您想要文本,则可以将nullable关键字添加到参数中,如下所示:

- (void)methodThatShouldntNeedAnnotationWithText:(nullable NSString *)txt;
Run Code Online (Sandbox Code Playgroud)