“此函数声明不是原型”。 Xcode 9中的警告 [英] "This function declaration is not a prototype" warning in Xcode 9

查看:1066
本文介绍了“此函数声明不是原型”。 Xcode 9中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode 9时,有一些编译器警告说这个函数声明不是原型。它建议将 void 添加到方法体,它将解决它。我遇到的问题是系统API也会抛出那些警告,如 UIApplication 委托方法:

When using Xcode 9, there are some compiler warnings saying This function declaration is not a prototype. It suggests to add void to the method body, which will resolve it. The issue I am having is that those warnings are also thrown for system-API's like UIApplication delegate-methods:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)())completionHandler

这可以通过以下方式解决:

This could be resolved by the following:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void (^)(void))completionHandler

现在我想知道委托方法是否仍然可以在长时间内工作-term或Apple将在以后的iOS 11 Beta版本中插入 void 。我很好奇,因为如果我包含 void 正文,Xcode会抱怨不匹配的方法选择器(这是有意义的)。到目前为止,有人遇到过同样的问题吗?

Now I am wondering if the delegate methods will still work on the long-term or Apple will insert the void in later iOS 11 Beta versions. I am curious because if I include the void body, Xcode will complain about mismatching method-selectors (which makes sense). Did someone experience the same issue so far?

推荐答案

带空括号的块声明:

void (^)()

与函数指针具有相同的语义空括号:

has the same semantics as a function pointer with empty parenthesis:

void (*)()

这并不意味着没有参数。这意味着没有指定参数,因此它打开了bug的方式,因为你可以通过以下方式调用它:

It does not mean that there are no arguments. It means the arguments are not specified, therefore it opens the way to bugs since you can call it in the following ways:

void (^block)() = ...
block();
block(10);
block(@"myString");

当声明没有参数的块时,请始终使用:

When declaring blocks with no parameters, always use:

void (^)(void)

Apple并没有在任何地方正确地做到这一点,并且出于兼容性原因,它们可能不会为旧API修复它。你必须在那里保持警告,直到你转移到更新的API。

Apple was not doing that correctly everywhere and they are not probably fixing that for old APIs for compatibility reasons. You will have to keep that warning there until you move to the newer API.

你也可以关闭那个警告( -Wstrict-prototypes ):

You can also turn off that warning (-Wstrict-prototypes):

或使用 #pragma (感谢 @davidisdk ):

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"

- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
   withResponseInfo:(NSDictionary *)responseInfo
  completionHandler:(void (^)())completionHandler {

}
#pragma clang diagnostic pop

参见LLVM讨论此处 openradar 上的错误。

See the LLVM discussion here or the bug on openradar.

请注意,API的内部工作没有变化,所有代码仍然有效。我们只会知道API不是那么好。

Note that's there was no change in the internal working of the APIs, all code will still work. We will only know that the API is not as good as it should be.

这篇关于“此函数声明不是原型”。 Xcode 9中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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