Skip to main content

Swift singletons are exposed in the Cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel. So it makes more sense as a class function to mirror this behaviourbehavior, rather than a class variable as some other solutions use,. e.g.:

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance().

Swift singletons are exposed in the Cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use, e.g.

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance().

Swift singletons are exposed in the Cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(). So it makes more sense as a class function to mirror this behavior, rather than a class variable as some other solutions. e.g:

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance().

Active reading (e.g. <http://en.wikipedia.org/wiki/Cocoa_%28API%29>).
Source Link
Peter Mortensen
  • 31.5k
  • 22
  • 110
  • 134

Swift Singletonssingletons are exposed in the cocoaCocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use, e.g.

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance().

Swift Singletons are exposed in the cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use e.g.

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance()

Swift singletons are exposed in the Cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use, e.g.

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance().

change to a static let within the class
Source Link
Ryan
  • 5.5k
  • 1
  • 42
  • 37

Swift Singletons are exposed in the cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use e.g.

class MyClass {

    private static let _sharedInstance = MyClass()
 
class MyClass {
    class func sharedInstance() -> MyClass {
        return _sharedInstance
    }
}

Retrieve the singleton via MyClass.sharedInstance()

Swift Singletons are exposed in the cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use e.g.

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

Retrieve the singleton via MyClass.sharedInstance()

Swift Singletons are exposed in the cocoa frameworks as class functions, e.g. NSFileManager.defaultManager(), NSNotificationCenter.defaultCenter(), so I feel it makes more sense as a class function to mirror this behaviour, rather than a class variable as some other solutions use e.g.

class MyClass {

    private static let _sharedInstance = MyClass()

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

Retrieve the singleton via MyClass.sharedInstance()

formatting
Source Link
Ryan
  • 5.5k
  • 1
  • 42
  • 37
Loading
Source Link
Ryan
  • 5.5k
  • 1
  • 42
  • 37
Loading