LoginSignup
12

More than 5 years have passed since last update.

Swift 4で@objcに関するwarningが出たときの対処

Last updated at Posted at 2017-11-11

warningの内容

Swift3プロジェクトをSwift4へマイグレーションさせた際に、以下のようなwarningが出る場合があります。

image.png

The use of Swift 3 @objc inference in Swift 4 mode is deprecated. Please address deprecated @objc inference warnings, test your code with “Use of deprecated Swift 3 @objc inference” logging enabled, and then disable inference by changing the "Swift 3 @objc Inference" build setting to "Default" for the "ターゲット名" target.

warningの説明

まず、Swift4の@objcについてはこちらの記事で説明しています。
Qiita | attributeまとめ[Swift4対応]

Swift3では@objcが明示的に付与されてない定義にも@objcが暗黙的に推論され、付与されていました。Swift4ではSwift3での@objc推論が非推奨となったため、設定を見直したほうが良いという警告です。この警告がでている際は、Swift3の@objc互換モードでビルドされるため、非推奨の@objc推論は有効になっています。

対応方法

TargetのBuild Settings設定の中に、Swift3 @objc Infenceという項目があるので、これをDefaultに変更します。

Swift3 @objc Infenceの設定値 説明
Default Swift4でのデフォルト値(@objc推論が無効)
On Swift3での@objc推論を有効にする(非推奨)
Off Swift3での@objc推論を無効にする

(DefaultとOffは何が違うのだろう...🤔)

image.png

なお、@objc推論が利用されているエントリポイントをログ出力により調べる場合には、以下の記事が参考となります。
The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

SWIFT_DEBUG_IMPLICIT_OBJC_ENTRYPOINT environmental variable to values from 1 to 3 to see the usages of the Objective-C entry points in the logs.

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12