mm24
mm24

Reputation: 9596

How to allocate static object defined in a Swift class inside an objective-C class?

How do I access to the shared instance of "SharedManager" declared in my Swift file from my Objective-C class?

Steps:

I have declared an object in a Swift file as following:

Swift Class Declaration:

From this [instructions][1] I did:

// Swift class
class SharedManager{
    static let sharedInstance = SharedManager()
}

From Objective-C class:

In AppDelegate.m:

#import "ProjectName-Swift.h"

@class SharedManager;
@interface AppDelegate ()
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// How do I access to the shared instance of "SharedManager" ?

}    

  [1]: http://stackoverflow.com/questions/24024549/using-a-dispatch-once-singleton-model-in-swift

Upvotes: 1

Views: 1889

Answers (4)

valvoline
valvoline

Reputation: 8117

Maybe late to the party, but you should also prepend the @objc marker also for the shared instance:

@objc
class VoiceShortcutsManager: NSObject {
    @objc static let shared = VoiceShortcutsManager()

    private override init() {
        super.init()
        updateVoiceShortcuts(nil)
    }

...

}

This way you can safely access the shared instance in objective-c:

...

[VoiceShortcutsManager shared];

...

Upvotes: 1

Bassam
Bassam

Reputation: 856

 class MyClass { 
    private static let _sharedInstance = MyClass() 

    class func sharedInstance() -> MyClass { return _sharedInstance } 
} 

Retrieve the singleton via

MyClass.sharedInstance()

Upvotes: -1

Alexander
Alexander

Reputation: 63271

To use a Swift class in Objective C, it must inherit from an Objective C class (such as NSObject), or be marked with @objc.

Either:

@objc class SharedManager {
    static let sharedInstance = SharedManager()
}

Or:

class SharedManager: NSObject {
    static let sharedInstance = SharedManager()
}

Then you can use it just like any other Objective C class:

SharedManager *manager = SharedManager.sharedInstance;

Upvotes: 1

mz2
mz2

Reputation: 4702

You need to do the following:

  1. #import "ProjectName-Swift.h" in the Objective-C implementation file (.m) where you want to refer to the Swift type.
  2. Subclass your SharedManager from NSObject:
class SharedManager: NSObject {
    static let sharedInstance = SharedManager()
}

This will work at least with Swift 2.3 and 3 (i.e. Xcode 8 & current macOS / iOS SDK versions, with or without "Use Legacy Swift Language Version" on in build configuration). I fail to remember if prior to Swift 2.3 you can make the above work with additionally marking the class with the @objc attribute or whether static variables declared in Swift are entirely unavailable to Objective-C code?

Upvotes: 0

Related Questions