【Objective-C】’void (^ _Nonnull __strong)()’ vs ‘void (^__strong _Nonnull)(void)’

今日は、ある時出くわしたどうにも解決出来ないワーニングについて、やっと解決方法を見つけたのでご紹介します。とは言え根本解決ではないですが・・・ワーニングを出さなくする感じです。暫定対応ですね。

completionHandlerの定義でワーニング

UIApplicationDelegateに定義されているメソッドで、以下のような物があります。ローカル通知からのアクションのハンドラーメソッドで、必要に応じて実装をすると思います。

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler

 
ある時からこのメソッドの最後のパラメータにワーニングが出るようになりました。以下のようなワーニングです。

This block declaration is not a prototype
Insert 'void'

 
「Insert ‘void’」にはfixボタンが表示されているので、そいつをクリックすると、「(void(^)())」が「(void(^)(void))」になります。で、これで解決か!!と思いきや、これをやってもさらに以下のような別のワーニングが表示されてしまいます。

Conflicting parameter types in implementation of 'application:handleActionWithIdentifier:forLocalNotification:completionHandler:': 'void (^ _Nonnull __strong)()' vs 'void (^__strong _Nonnull)(void)'

 
このワーニングを消すことが出来ずに困っていました。

解決方法

まず、最初のfixをもとに戻します。「(void(^)(void))」を「(void(^)())」に戻します。そして以下のようにメソッドの定義にコメントをはさみます。2行目3行目5行目になります。

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstrict-prototypes"
completionHandler:(void(^)())completionHandler
#pragma clang diagnostic pop
{
// ゴニョゴニョ
}

 
どうでしょうか? ワーニングが消えましたでしょうか? 確かに消えますが、コメントが増えるのはまた一長一短な感じですかね。う〜ん悩ましい

参考サイト

Methods required by UIApplicationDelegate do not conform to -Wstrict-prototypes

関連記事

SwiftでOpenALの簡単サンプルソース

【Ubuntu】Ubuntuを19.04にアップグレードしてみた

【zsh】ラズパイに入れてみた

【GraalVM】ちょっと弄ってみた

【Google Home】Google Homeにテキストを読み上げさせたい

【Swift】'applicationFrame' was deprecated...