Interop

Interoperability with Swift

Intro

Swift keywords for inter-op with Objective-C
dynamic_runtime

swift-objective-c-interoperability-and-best-practices

Bridge header

Creating a bridge header for connecting both swift & objective c environment which could be auto prompted by Xcode.

Also swift won’t have to import in independent files with .swift file extension. It gets auto imported from bridge header file.
SO

Swift to Objc Bridge

Class

class SomeClass { }
// bridge to Obj-C
class SomeClass: NSObject { }

Protocol Interface

public protocol CSSDelegate: AnyObject {
    func handleChange(result: SomeClass)
}
// bridge to Obj-C
@objc
public protocol CSSDelegate: AnyObject {
    func handleChange(result: SomeClass)
}

Adding Objective-C to Swift codebase

SO | how-do-i-call-objective-c-code-from-swift

import swift in objective-c

Considerations - from SO | import swift code to Objective C

  • If your product name contains spaces, replace them with underscores (e.g. My Project becomes My_Project-Swift.h)
  • If your target is a framework, you need to import <ProductName/ProductName-Swift.h>
  • Make sure your Swift file is a member of the target
// CFrameworkPlatform
@objc
public protocol CProtocol: AnyObject {
    func handleChange(result: String)
}
#import <CFrameworkPlatform/CProtocol-Swift.h>

Caveat if its an objectiveC inter-op from swift in a framework then it gets automatically created by xcode build process named as customFrameworkName-Swift.h

So you should just import it using

#import <CFrameworkPlatform/CFrameworkPlatform-Swift.h>