Reputation: 8609
I've created a singleton object in Swift 4.2 and am attempting to access it (call a couple methods) in an Objective-C class. However, when calling the instance in Objective-C, the app crashes with the following:
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
My Swift singleton definition isn't anything unusual, and it works fine when called only from other Swift classes. Here's what it looks like:
@objc public static let sharedManager = SessionManager()
It doesn't seem to be an issue with language interoperability in the project either: non-singleton files don't cause a crash when accessed from Objective-C. I'm also not sure that this Objective-C code is so wild that it should be causing it either:
// Have attempted both flavors here, the crash is coming from the call to "sharedManager", which the runtime thinks is nil
SessionManager *manager = [SessionManager sharedManager];
[manager setSession:nil];
Any ideas on how to fix this? What am I missing? Why would the singleton instance ever be nil?
Edit: I've attempted expanding the implementation of the Swift Singleton to this, and its still not working:
@objc final class SessionManager: NSObject {
@objc public static let _sharedManager = SessionManager()
private override init() {
super.init()
}
@objc public class func sharedManager() -> SessionManager {
return SessionManager._sharedManager
}
}
Upvotes: 3
Views: 560
Reputation: 8609
When accessing a Singleton in Objective-C for the first time, each variable stored in the class must be initialized.
I found that what was happening (and what made this so frustrating) was that one of the objects being initialized made calls back to the singleton object in its own initializer -- thus forming an infinite loop that the runtime couldn't exit.
UPDATE: A bug report radar has been filed with the Swift team.
Upvotes: 2