"This function declaration is not a prototype" warning in Xcode 9

Hans Knöchel picture Hans Knöchel · Jun 10, 2017 · Viewed 43.5k times · Source

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

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?

Answer

Sulthan picture Sulthan · Jun 10, 2017

The block declaration with empty parenthesis:

void (^)()

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

void (*)()

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 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.

You can also turn off that warning (-Wstrict-prototypes): enter image description here

or using #pragma (thanks @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

See the LLVM discussion here or the bug on openradar.

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.