Objective C Singleton method not available in Swift Interface

I've been looking for documents of reasons of why a singleton method definition in Objective-C class is not available on the Swift interface on Xcode.

The Objective-C class is defined like this

/*
* A general accessor across the sample App to remove the dependency of QPSLibraryManager from resusable components.
*/
@interface QPSSharedAccessor : NSObject
/*
* Required by QPSLibraryManager and some UI components.
*/
@property (nonatomic, strong) QPSApplicationConfiguration *qpsConfiguration;
/*
* Provides Commands to app
*/
@property (nonatomic, strong) id<QPSAppController> appController;;
/*
* Shared singleton.
*/
+ (instancetype)sharedAccessor;
/*
* Returns access to a configuration manager
*/
- (QPSConfigurationManager *)configurationManager;
@end


On Swift Interface, its defined like this

/*
* A general accessor across the sample App to remove the dependency of QPSLibraryManager from resusable components.
*/
open class QPSSharedAccessor : NSObject {
  
    /*
     * Required by QPSLibraryManager and some UI components.
     */
    open var qpsConfiguration: QPSApplicationConfiguration!
  
    /*
     * Provides Commands to app
     */
    open var appController: QPSAppController!
  
    /*
     * Returns access to a configuration manager
     */
    open func configurationManager() -> QPSConfigurationManager!
}


I'm expected to see the sharedAccessor() singleton method on Swift but it is missing as you can see. Calling the said method on a separate swift file results in a compiler error, saying that the sharedAccessor() method doesn't exist. Converting everything to Swift is not viable btw. Advice on fixing this problem?


PS: Posted on StackOverflow for more publicity

Replies

singleton are not fully supported in Swift.


Here is how I implement :


class GlobalData {

class var sharedGlobal : GlobalData {

struct Singleton {

static let instance = GlobalData()

}

return Singleton.instance;

}


// All the properties and methods follow


}

Something is definitely going wrong with the Objective-C importer here. If you rename that class method to

sharedAccessorX
, it becomes visible to Swift. Please file a bug with your example and then post the bug number here, just for the record.

Two things:

  • You’ll get a more Swift-friendly interface if you use the new class properties feature in Objective-C (example below).

  • Are you sure you want this to be

    instancetype
    ? That’s pretty unusual for a singleton because it means that a subclass has to either a) override the method to return an instance of the correct type, which makes the singleton not ‘single’, or b) not override the method, in which case it’s not following the type promise.

    I should note, however, that this has no bearing on the Swift issue you’re seeing.

// this:

@property (class, readonly) QPSSharedAccessor * shared;

// yields this:

open class var shared: QPSSharedAccessor { get }

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"