指针缺少可为空类型说明符 [英] Pointer is missing a nullability type specifier

查看:136
本文介绍了指针缺少可为空类型说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 7 GM中,我开始收到此警告:

In Xcode 7 GM I started to get this warning:

指针缺少可空性类型说明符(_Nonnull,_Nullable或_Null_unspecified)

Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)

在以下函数声明(NSUserDefaults扩展名)中

In the following function declaration (NSUserDefaults extension)

- (void)setObject:(nullable id)value
           forKey:(NSString *)defaultName
    objectChanged:(void(^)(NSUserDefaults *userDefaults, id value))changeHandler
    objectRamains:(void(^)(NSUserDefaults *userDefaults, id value))remainHandler;

为什么显示此警告,我该如何解决?

Why this warning is showing and how should I fix it?

推荐答案

您还需要为处理程序/块指定nullable

You need to specify nullable also for the handlers/blocks

- (void)setObject:(nullable id)value
           forKey:(nonnull NSString *)defaultName
    objectChanged:(nullable void(^)(NSUserDefaults *userDefaults, id value))changeHandler
    objectRamains:(nullable void(^)(NSUserDefaults *userDefaults, id value))remainHandler;

为什么?这是由于斯威夫特. Swift允许使用可选参数(?),而Objective-C则不允许.这样做是作为两者之间的桥梁,以便Swift编译器知道那些参数是可选的. 'Nonnull'将告诉Swift编译器该参数是必需的.可以为空的空值

Why? It's due to Swift. Swift allows optional parameters (?), which Objective-C does not. This is done as a bridge between the both of them for the Swift compiler to know those parameters are optional. A 'Nonnull' will tell the Swift compiler that the argument is the required. A nullable that it is optional

有关更多信息,请阅读: https://developer.apple.com/swift/blog/?id=25

For more info read: https://developer.apple.com/swift/blog/?id=25

这篇关于指针缺少可为空类型说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆