如何与iWatch应用程序共享课程?

时间:2015-07-09 20:51:31

标签: objective-c iphone swift watchkit

我正在为我的iPhone应用程序开发一款手表应用程序。 iPhone应用程序是在objective-c中开发的,手表应用程序正在迅速开发。我需要与我的手表扩展共享我的部分课程,所以我将这些课程添加到两个不同的目标。这是正确的方法吗?

1 个答案:

答案 0 :(得分:0)

我会使用单例并构建它以在Swift和Objective-C中访问。然后可以在应用程序范围内调用单例。这是我从SO question找到的潜在解决方案。您可能需要的任何信息都可以从任何地方分配给单身人士。

@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
    }