5

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.

Community
  • 1
  • 1
Guy Daher
  • 5,526
  • 5
  • 42
  • 67

2 Answers2

5

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.

zpasternack
  • 17,838
  • 2
  • 63
  • 81
  • What if I'm using Generics in the private section of the app? And in public section I will use compatible things.. It will be good? Can I write swift API with objective c compatibility with that way? – Roman Derkach Sep 10 '17 at 20:23
  • 1
    The links are broken (they redirect to Swift), but can be found [here](https://web.archive.org/web/20170703161422/https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html), archived from 2017. – Ben Butterworth Jun 06 '21 at 10:56
0

There are more things good to know beforehand.

  1. You cannot extend swift class by objective c class.
  2. You cannot use @objc on generic swift class or method (you can use generics in @objc swift class on methods though)

So the api will be more limited than if written in objective c as it support generics (though people like to say not real generics etc...) and not to be able to extend any class you provide in your api can be quite limitation too, but still it's doable.

You just have to put @objc everywhere and compiler will say you very quickly what is not supported. Also casting can be quite tricky sometimes... I ended up to cast like this because I was reciving objective c generic class from swift in swift code

as! CSResponse<AnyObject> as! (CSResponse<AnyObject> & CSListData) 

This is working code, but direct cast is imposible.

Best is to write pure swift for swift and use objective c libs as needed otherwise you will and up fighting to write one line of code. (like me :))

Renetik
  • 5,887
  • 1
  • 47
  • 66