指针缺少函数接口上的可空性类型说明符

Yuc*_*ong 9 objective-c ios

最初,我有一些非常简单的事情如下.一个自定义类别,UIAlertAction以便我可以简单地创建一个好的行动[UIAlertAction okay];

@interface UIAlertAction (Custom)
+ (UIAlertAction *)okay;
@end

@implementation UIAlertAction (Custom)
+ (UIAlertAction *)okay
{
    return [UIAlertAction actionWithTitle:@"Ok"
                                    style:UIAlertActionStyleCancel
                                  handler:nil];
}
@end
Run Code Online (Sandbox Code Playgroud)

后来,我决定我也希望有一个完整的人,所以界面变成了这样:

@interface UIAlertAction (Custom)
+ (UIAlertAction *)okay;
+ (UIAlertAction *)okayWithHandler:(void (^ __nullable)(UIAlertAction *action))handler;
@end
Run Code Online (Sandbox Code Playgroud)

并且我开始在第一行收到警告信息:

指针缺少可空性类型说明符

在此输入图像描述

我认为理解可空的想法(老实说,这个名字很有描述性).但是我不明白的是为什么__nullable在第二个函数上添加一个将使第一个函数具有可空的说明符的原因?

mat*_*att 25

但我不明白的是为什么在第二个函数上添加__nullable会使第一个函数有可空的说明符?

这只是因为只要您在标题中提供任何可为空性信息,就应该为该标题中的所有内容提供可为空性信息.在可空性方面,未指定整个API或指定整个API.编译器只是警告你,你已经完成了半工作.