objective-cswiftcocoapodsobjective-c-swift-bridge

Using Swift Project (Siren) in an Objective-C iOS Application


I am trying to use a popular swift library, Siren, in an iOS Objective-C application. I have included the library as a framework using cocoapods, I ran pod init and then pod install with a podfile like this:

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'MyObjectiveCApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for MyObjectiveCApp
  pod 'Siren'

end

and I've imported the swift code into the AppDelegate.m file like so:

#import <Siren/Siren-Swift.h>

I can now access the Siren class, but I can't seem to reference the shared static property on the Siren class. I have tried [Siren shared] and Siren.shared but neither is working. Here is the first few lines of the Siren class:

import UIKit

/// The Siren Class.
public final class Siren: NSObject {
    /// Return results or errors obtained from performing a version check with Siren.
    public typealias ResultsHandler = (Result<UpdateResults, KnownError>) -> Void

    /// The Siren singleton. The main point of entry to the Siren library.
    public static let shared = Siren()

Is there any documentation or examples of using Siren in an Objective-C iOS application? Is this possible? I know I could use Harpy, but since it is no longer supported, I was trying to avoid it if possible. I have researched using Swift in Objective-C code, but I couldn't find anything specifically related to accessing a static let property on a class. I found this helpful answer, but it mentions global variables, not static class properties.


Solution

  • The reason why you can't access shared property, is because it doesn't get exported to Siren-Swift.h header (you can check generated header file).

    That's because Siren class is not explicitly marked as @objc. The class itself is exported, because it's subclass of NSObject, but the properties are not exported by default. I think we can't solve it on our own.

    What we can do is a workaround: