Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Swift OSS library compatible with Objective-C

I am building a library in Swift, and it has to support Objective-C.

I already checked this answer which recommends to write the library in Objective-C but the requirements that were given to me are to write the library in Swift. I am delivering the library in source form, so the argument there (against writing the library in Swift) about unstable ABI should not apply in my case.

So I've heard that in order to make this Swift library work for Objective-C, I will have to avoid using the advanced features in Swift that are not available in Objective-C. Examples of these are:

  • Generics
  • Structs
  • All Swift classes must derive from NSObject

So my 2 questions are:

  1. Where Can I find an exhaustive list for those constraints?
  2. How can I quickly test that my library is compatible with Objective-C? I am not familiar at all in the interoperability topic of Swift and Objective-C. Not a lot articles that I could find online. Is the official Apple docs sufficient? and which parts can help?

I appreciate all the help here.

like image 834
Guy Daher Avatar asked Feb 22 '17 20:02

Guy Daher


1 Answers

The most comprehensive list of Swift features not available from Objective-C is in the Swift Type Compatibility section of Apple's Using Swift with Cocoa and Objective-C guide.

Quoting from there, the list of exclusions are as follows:

  • Generics
  • Tuples
  • Enumerations defined in Swift without Int raw value type
  • Structures defined in Swift
  • Top-level functions defined in Swift
  • Global variables defined in Swift
  • Typealiases defined in Swift
  • Swift-style variadics
  • Nested types
  • Curried functions

The whole guide is worth reading, but I'd pay particular attention to the Mix and Match section which describes calling Swift from Objective-C and vise-versa, including external frameworks.

I would definitely recommend doing as @Mike Taverne suggests: make a suite of unit tests in Objective-C which exercise the APIs you've developed in Swift. That's the best way to make sure it all works as expected.

like image 134
zpasternack Avatar answered Nov 16 '22 20:11

zpasternack