Call a Swift Singleton from Objective-C

后端 未结 5 618
小蘑菇
小蘑菇 2020-12-06 00:18

I\'m having some trouble accessing a Swift Singleton from Objective-C.

@objc class SingletonTest: NSObject {

    // swiftSharedInstance is not accessible fr         


        
相关标签:
5条回答
  • 2020-12-06 00:56

    To make members of the SingletonTest class accessible (swiftSharedInstance is a member of this class), use @objcMembers modifier on the class, or add @objc modifier directly on the swiftSharedInstance:

    @objc @objcMembers class SingletonTest: NSObject {
    
        // swiftSharedInstance is not accessible from ObjC
        class var swiftSharedInstance: SingletonTest {
            struct Singleton {
                static let instance = SingletonTest()
            }
            return Singleton.instance
        }        
    }
    

    Or:

    @objc class SingletonTest: NSObject {
    
        // swiftSharedInstance is not accessible from ObjC
        @objc class var swiftSharedInstance: SingletonTest {
            struct Singleton {
                static let instance = SingletonTest()
            }
            return Singleton.instance
        }        
    }
    
    0 讨论(0)
  • 2020-12-06 01:04

    For now I have the following solution. Maybe I am overlooking something that would enable me to access "swiftSharedInstance" directly?

    @objc class SingletonTest: NSObject {
    
        // swiftSharedInstance is not accessible from ObjC
        class var swiftSharedInstance: SingletonTest {
        struct Singleton {
            static let instance = SingletonTest()
            }
            return Singleton.instance
        }
    
        // the sharedInstance class method can be reached from ObjC
        class func sharedInstance() -> SingletonTest {
            return SingletonTest.swiftSharedInstance
        }
    
        // Some testing
        func testTheSingleton() -> String {
            return "Hello World"
        }
    
    }
    

    Then in ObjC I can get the sharedInstance class method (after importing the xcode generated swift header bindings)

    SingletonTest *aTest = [SingletonTest sharedInstance];
    NSLog(@"Singleton says: %@", [aTest testTheSingleton]);
    
    0 讨论(0)
  • 2020-12-06 01:07

    Nicky Goethlis's answer is correct but I just want to add another way of Singleton creation termed as One line Singleton" in Swift which I came across recently and it does not use Struct:

    Singleton.swift

    @objc class Singleton: NSObject {
    
      static let _singletonInstance = Singleton()
      private override init() {
        //This prevents others from using the default '()' initializer for this class.
      }
    
      // the sharedInstance class method can be reached from ObjC. (From OP's answer.)
      class func sharedInstance() -> Singleton {
        return Singleton._singletonInstance
      }
    
      // Some testing
      func testTheSingleton() -> String {
        return "Hello World"
      }
    }
    

    SomeObjCFile.m

    Singleton *singleton = [Singleton sharedInstance];
    NSString *testing = [singleton testTheSingleton];
    NSLog(@"Testing---> %@",testing);
    
    0 讨论(0)
  • 2020-12-06 01:12

    Swift 5 and above

    final class Singleton: NSObject {
    
        @objc static let shared = Singleton()
    
        @objc var string: String = "Hello World"
    
        private override init() {}   
    }
    

    use in Objective-C

    NSLog("Singleton String = %@", [Singleton shared].string]);
    
    0 讨论(0)
  • 2020-12-06 01:19

    You pretty much have it. To use Swift classes in Obj-C you both need to #import "SingletonTest-Swift.h the generated header or forward declaration with @class MySwiftClass.

    Additionally the class needs to inherit from an Obj-C class like you have don here with NSObject or be marked with @objc to expose it. You don't need to do both though, @objc is there to be a more granular option when choosing things to expose.

    Apple has some good documentation on all of this and there are two different WWDC sessions you can watch on the topic of Obj-C interoperability as well.

    0 讨论(0)
提交回复
热议问题