Skip to content

Instantly share code, notes, and snippets.

@gatamar
Last active May 21, 2020 13:38
Show Gist options
  • Save gatamar/f4d255ee8436fadd6267a080f0ae4a1e to your computer and use it in GitHub Desktop.
Save gatamar/f4d255ee8436fadd6267a080f0ae4a1e to your computer and use it in GitHub Desktop.

Checklist for adding Swift support to Objc project

  1. Add (auto-generated) bridging header ProductName-Bridging-Header.h.
  2. Add SWIFT_VERSION to Build Settings -> Swift Compiler - Language.
  3. Fix Multiple commands produce '.../Objects-normal/x86_64/ProductName.swiftmodule' - target ProductNames should differ.
  4. Set "Defines Modules" to "Yes" in Build Settings -> Packaging.
  5. #import "Objective-C Generated Interface Header Name"
  6. Remove #import "ProductName-Swift.h" from headers to implementation, add forward declaration to headers.

The "rules" of converting Swift names to Objc

I had such constructor in MyClass:

public override init(userDefaults: UserDefaults) {

And I wanted to call it in Objc like this:

MyClass* obj = [[MyClass alloc] initWithUserDefaults:NSUserDefaults.standardUserDefaults];

It didn't work, while this did:

@objc convenience init(userDefaults: UserDefaults)

More comments about optional value type bridging/optional reference type bridging is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment