Using Objective-C Singleton in Swift file within Objective-C project

54 views Asked by At

I've got my project written in Objective-C. I am learning about bridging.

I bridged successfully Swift into my Objective-C project. I did some VC pushing between Objective-C VC and Swift VC. Everything works as it supposed to.

However, I created the reference to my Objective-C NetworkManager (I used Singleton pattern) in my Swift file like that:

let networkManager = NetworkManager.sharedManager()

For some reason I cannot use methods from it.

networkManager.getUserInfo() // .getUserInfo() will not appear!

Just to show you how I've created my NetworkManager as a Singleton. I found that solution online and it was quite old, if you have any better solution, I would be more than happy to get some insight:

+ (id)sharedManager {
    static NetworkManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

-(id)init {
    if (self = [super init]) {
        someProperty = @"Default Property Value";
        self.cache = [[NSCache alloc] init];
    }
    return self;
}

Not sure what is the issue here. Thank you all for the help.

1

There are 1 answers

1
Jakub Gawecki On

For those who may look for the answer in the future.

I've been advised to not use id type when bridging with Swift. I've changed:

+ (id)sharedManager {

to

+ (NetworkManager *)sharedManager

And now everything works fine.