Knowledgebase

Swift and Objective-C Interoperability Print

  • appletechnology, apple, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Working with existing code.

WHY IT MATTERS

Enormous amounts of existing code and many system frameworks are written in the older language.

WHAT IS AUTOMATIC

Swift can use Objective-C types, and much of the older API appears translated.

WHAT TRANSLATION DOES

Renames methods to Swift conventions Converts certain types Marks values as optional where nullability is declared

WHAT NULLABILITY ANNOTATIONS DO

Tell Swift whether a value can be nothing.

WHAT HAPPENS WITHOUT THEM

Values arrive as implicitly unwrapped, which can crash unexpectedly.

WHAT TO DO IN MIXED PROJECTS

Add nullability annotations to older headers.

WHAT EXPOSING SWIFT TO OBJECTIVE-C REQUIRES

Marking types and members, and inheriting from the base object type where needed.

WHAT CANNOT BE EXPOSED

Swift-only features: enumerations with associated values, generics, structures.

WHAT THAT MEANS

An interface used from both must be expressible in both.

WHAT TO DO WHEN ADOPTING SWIFT GRADUALLY

Convert file by file, starting with self-contained code.

WHAT TO AVOID

Converting everything at once.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot