1

I'm working on a watch app for my iPhone app. The iPhone app has been developed in objective-c and the watch app is being developed in swift. I need to share part of my classes with my watch extension so I am adding the classes to the two different targets. Is this a correct approach?

agy
  • 2,804
  • 2
  • 15
  • 22

1 Answers1

0

I would use a singleton and build it to access in both Swift and Objective-C. The singleton can then be invoked app-wide. Here's a potential solution I found from this SO question. Any information you could need can be assigned to the singleton from anywhere.

@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
    }
Community
  • 1
  • 1
Cole
  • 2,641
  • 1
  • 16
  • 34