Mixing Swift and Objective-C

Platform & distribution Mid-level Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Pick the fix

An Objective-C file can't see your Swift class Uploader. Fix?

Options
Hint 1 of 1

Which header exposes Swift to Objective-C?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Make Uploader inherit from NSObject with @objc members, and import "MyApp-Swift.h" in the .m file

Correct. That exposes the class through the generated header.

Why not the others?
  • B Add Uploader to the bridging header. The bridging header goes the other way, Objective-C into Swift.
  • C Make Uploader a struct. Structs aren't visible to Objective-C.
  • D Rename the file to Uploader.m. Swift code can't simply be renamed.

30-second answer

Objective-C reaches Swift through the generated <ModuleName>-Swift.h header, which includes Swift declarations that are exposed with @objc (classes need to inherit from NSObject). Swift reaches Objective-C in an app target through a bridging header, or in a framework through its umbrella header and module map. Nullability annotations and NS_SWIFT_NAME make Objective-C APIs read well in Swift.

Practise saying it · 30 s. No recording, just a timer

Explain Swift and Objective-C interop.

Say your answer out loud, then check which of these points you covered:

  • Generated -Swift.h
  • Bridging header
  • @objc and NSObject
  • Nullability

Deep dive

Swift-only features (structs, enums with payloads, generics) aren't visible to Objective-C.

@objcMembers exposes all members of a class.

Use NS_REFINED_FOR_SWIFT to wrap awkward APIs.

Swift 5.9+ can also interoperate with C++ directly.

Common mistakes

  • Expecting structs to be visible in Objective-C.
  • Missing nullability annotations, which make everything implicitly unwrapped.

Senior take & follow-ups

Seniors know both directions and how to make the boundary pleasant.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why are Objective-C types implicitly unwrapped in Swift?

    Show hint

    No nullability annotations.

  2. What isn't visible to Objective-C?

    Show hint

    Structs, generics and enums with associated values.

Suggest a fix if something here is wrong or out of date.