1
我正在爲我的iPhone應用程序開發一款手錶應用程序。 iPhone應用程序已在Objective-C中開發,手錶應用程序正在快速開發中。我需要與我的手錶擴展分享我的課程的一部分,所以我將這些課程添加到兩個不同的目標中。這是一個正確的方法嗎?如何與iWatch應用程序共享課堂?
我正在爲我的iPhone應用程序開發一款手錶應用程序。 iPhone應用程序已在Objective-C中開發,手錶應用程序正在快速開發中。我需要與我的手錶擴展分享我的課程的一部分,所以我將這些課程添加到兩個不同的目標中。這是一個正確的方法嗎?如何與iWatch應用程序共享課堂?
我會使用一個單例並構建它來訪問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
}