Call a Swift Singleton from Objective-C

后端 未结 5 617
小蘑菇
小蘑菇 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 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]);
    

提交回复
热议问题