从Objective-C调用Swift Singleton [英] Call a Swift Singleton from Objective-C
本文介绍了从Objective-C调用Swift Singleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从Objective-C访问Swift Singleton遇到麻烦.
I'm having some trouble accessing a Swift Singleton from Objective-C.
@objc class SingletonTest: NSObject {
// swiftSharedInstance is not accessible from ObjC
class var swiftSharedInstance: SingletonTest {
struct Singleton {
static let instance = SingletonTest()
}
return Singleton.instance
}
}
swiftSharedInstance无法访问.
swiftSharedInstance can not be reached.
推荐答案
目前,我有以下解决方案.也许我忽略了一些使我能够直接访问"swiftSharedInstance"的东西?
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"
}
}
然后在ObjC中,我可以获取sharedInstance类方法(在导入xcode生成的swift标头绑定之后)
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]);
这篇关于从Objective-C调用Swift Singleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文