最初,我有一些非常简单的内容如下。自定义类别UIAlertAction
,以便我可以通过[UIAlertAction okay]
创建一个好的操作;
@interface UIAlertAction (Custom)
+ (UIAlertAction *)okay;
@end
@implementation UIAlertAction (Custom)
+ (UIAlertAction *)okay
{
return [UIAlertAction actionWithTitle:@"Ok"
style:UIAlertActionStyleCancel
handler:nil];
}
@end
后来,我决定我也希望有一个完整的人,所以界面变成了这个:
@interface UIAlertAction (Custom)
+ (UIAlertAction *)okay;
+ (UIAlertAction *)okayWithHandler:(void (^ __nullable)(UIAlertAction *action))handler;
@end
并且我开始在第一行收到警告信息:
指针缺少可空性类型说明符
我认为理解可空的想法(老实说,这个名字很有描述性)。但我不明白的是为什么在第二个函数上添加__nullable
会使第一个函数有必要设置为nullable?
答案 0 :(得分:25)
但是我不明白为什么在第二个函数上添加__nullable会使第一个函数的nullable说明符成为必需?
这只是因为只要您在标头中提供任何可空性信息,就应该为该标头中的所有提供可空性信息。在可空性方面,未指定整个API或指定整个API。编译器只是警告你,你已经完成了半工作。