I would like to get opinion if I am doing this right. I am creating a helper class that is globally accessible to my whole project. I want it to be easily called like Facebook.loginUser()

My code is like this:

class Facebook: NSObject {
  class func LoginUser() {
    println("Login user...")

And on usage just call the class name and method:


instead of:

var FB: Facebook = Facebook()
| improve this question | | | | |

This design pattern is known as a Singleton, and you can read more about them here.

It's super easy to set up a singleton in Swift, and when I use this pattern I forward all method calls to the shared instance of the class.

class Singleton {
    static let sharedInstance = Singleton()

    // type method (aka class method), allows for easy-access code
    class func doSomething() {
        // forward the method and any parameters to the shared instance

    // instance method, visible through Singleton.sharedInstance
    func doSomething() {
        println("Did something.")

And the usage would be like so:


(Both call the same method, just the first one is more concise)


  • Apple is calling class methods in swift type methods
  • I use camelCaseForMethodNames because it is a convention (instead of CapitalizingEveryWord like class names)
  • People tend to have mixed opinions about Singletons (read more about that here, if you desire).
| improve this answer | | | | |
  • \$\begingroup\$ Read through class methods and instance method and seems like instance method is the way to go for me here. And yes I do camelCaseForMethodNames, it was a typo on my question. :) \$\endgroup\$ – SleepNot Jun 24 '15 at 5:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.