【问题标题】:Pointer is missing a nullability type specifier on function interface指针在函数接口上缺少可空性类型说明符
【发布时间】:2015-10-09 02:24:28
【问题描述】:

最初,我有一些非常简单的东西,如下所示。 UIAlertAction 的自定义类别,这样我就可以通过 [UIAlertAction okay] 创建一个好的操作;

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

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

后来,我决定我也想要一个完整的hander,所以界面变成了这样:

@interface UIAlertAction (Custom)
+ (UIAlertAction *)okay;
+ (UIAlertAction *)okayWithHandler:(void (^ __nullable)(UIAlertAction *action))handler;
@end

然后我开始在第一行收到警告消息:

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

我认为理解可以为空的概念(老实说,这个名字很有描述性)。但我不明白的是,为什么在第二个函数上添加 __nullable 会使第一个函数必须具有可为空的说明符?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

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

    这仅仅是因为只要您在标头中提供 any 可空性信息,您就应该为该标头中的所有内容 提供可空性信息。就可空性而言,要么未指定整个 API,要么指定了整个 API。编译器只是警告你,你已经完成了一半的工作。

    【讨论】:

    • 非常感谢!现在我明白了为什么我开始收到大量这样的警告,而且只在我的一门课上。我只是碰巧从另一个项目中复制了一些方法声明,其中有一些 __nullable 声明。我刚刚删除了该声明,所有警告都消失了!
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多