Fix: Swift 3 @objc Inference Deprecated Warning in Swift 4 and Later

Fix: Swift 3 @objc Inference Deprecated Warning in Swift 4 and Later

Written by

in

Quick answer: Change the ‘Swift 3 @objc Inference’ build setting (SWIFT_SWIFT3_OBJC_INFERENCE) from ‘On’ to ‘Default’ in your target’s Build Settings. Then add explicit @objc attributes to any Swift declarations that need Objective-C visibility, or use @objcMembers on classes that require full exposure.

Why this happens

Swift 3 automatically inferred @objc on many declarations in classes that inherited from NSObject, exposing them to the Objective‑C runtime without any annotation. This convenience came at a cost: the compiler generated Objective‑C entry points for every method and property, increasing binary size and preventing function overloading.

Starting with Swift 4 (Xcode 9), that automatic inference is deprecated. The build setting Swift 3 @objc Inference (SWIFT_SWIFT3_OBJC_INFERENCE) controls the behavior. In projects created before Swift 4 the setting defaults to On, which keeps the old behavior but emits the deprecation warning:

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 disable
Swift 3 @objc inference.

New Swift 4+ projects use Default (effectively Off), so only declarations you explicitly mark with @objc or @objcMembers are exposed to Objective‑C.

How to fix it

1. Switch the build setting to Default (recommended)

  1. Open the project navigator and select your target (not the project).
  2. Choose the Build Settings tab.
  3. Search for Swift 3 @objc Inference (or SWIFT_SWIFT3_OBJC_INFERENCE).
  4. Change the value from On to Default.
  5. Build the project. You will now see errors on every declaration that previously relied on implicit @objc.

This is the cleanest long‑term fix because it forces you to make Objective‑C exposure intentional.

2. Add explicit @objc to individual members

For each method, property, or initializer that must be visible to Objective‑C (e.g., target‑action selectors, KVO, @IBOutlet, @IBAction), prefix it with @objc:

class ViewController: UIViewController {
    @objc func handleTap(_ sender: UITapGestureRecognizer) { }
    @objc var dynamicProperty: String = ""
    @objc init(configuration: Config) { }
}

Only the annotated members get Objective‑C entry points, keeping the binary lean.

3. Use @objcMembers on a class (quick migration)

If a class has many members that need exposure and you want to avoid annotating each one, mark the whole class:

@objcMembers
class LegacyModel: NSObject {
    var name: String = ""
    func calculate() -> Int { return 0 }
    // everything above is now @objc
}

Trade‑off: Every member becomes @objc, increasing binary size and disabling Swift‑only features like function overloading for those members. Use this as a temporary step while you audit which members truly need Objective‑C visibility.

4. Enable the diagnostic logging (optional)

To see exactly which implicit @objc entries the compiler previously generated, enable the runtime logging flag:

  1. Edit the scheme (Product ▸ Scheme ▸ Edit Scheme).
  2. Select Run ▸ Arguments.
  3. Add -Swift3ObjCInferenceLoggingEnabled YES to Arguments Passed On Launch.
  4. Run the app; the console will list every inferred @objc entry.

This helps you decide which members deserve explicit @objc before you flip the build setting.

How to prevent it

  • Start new projects with the default setting — Xcode templates already set SWIFT_SWIFT3_OBJC_INFERENCE = Default.
  • Audit @objc usage during code review — only expose what Objective‑C code actually calls.
  • Prefer Swift‑only APIs — use Swift closures, protocol extensions, and @MainActor instead of selectors where possible.
  • Run the migration assistant — Xcode’s Edit ▸ Convert ▸ To Current Swift Syntax can add missing @objc annotations automatically in many cases.

FAQ

Does this warning become an error in newer Swift versions?

Yes. In Swift 4 the setting On produces warnings; in Swift 5 and later it produces errors. You must migrate to Default (or Off) and add explicit @objc before upgrading the Swift language version.

Can I keep the old behavior for a single target only?

Yes. The build setting is per‑target. You can leave a legacy framework target on On while your app target uses Default, but you will still see the deprecation warnings for that framework. The long‑term goal is to migrate every target.

What about @IBOutlet and @IBAction — do they need @objc?

No. @IBOutlet and @IBAction implicitly carry @objc semantics. You do not need to add @objc to them, and they will continue to work after you switch the build setting to Default.

Will adding @objc break Swift‑only features like method overloading?

Only for the specific members you mark @objc. Overloaded methods that are not exposed to Objective‑C remain unaffected. If you use @objcMembers on a class, all members in that class lose overloading capability.

Is there an automated way to add missing @objc annotations?

Xcode’s Convert to Current Swift Syntax (under the Edit menu) can insert @objc where the compiler detects a selector reference (#selector(...)) or an @IBOutlet/@IBAction. Run it after switching the build setting to Default to catch most cases automatically.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.