1

I found this in the Mozilla Firefox for iOS repository.

static let WebServerSharedInstance = WebServer()

class var sharedInstance: WebServer {
    return WebServerSharedInstance
}

Why would you create a new variable that just returns the same variable when you could do the same in one line like this?

static let sharedInstance = WebServer()
2
  • I have modified my answer, please make sure you still consider it as accepted. Commented Apr 8, 2017 at 7:37
  • Your answer is fantastic, giving two reasons why that might be. And thank you for giving me a heads up! Commented Apr 8, 2017 at 7:44

3 Answers 3

2

I have looked into the code and I think I have misunderstood the context:

class WebServer {
    static let WebServerSharedInstance = WebServer()

    class var sharedInstance: WebServer {
        return WebServerSharedInstance
    }
}

While you can always use WebServer.WebServerSharedInstance to access the singleton, a subclass of WebServer can override sharedInstance.

class MyServer : WebServer {
    override class var sharedInstance: MyServer {
        return MyServer()
    }
}

However, I have found no example in the code doing that so the original answer below is probably correct.

Original answer:

You are right. There is absolutely no reason to do that.

This has been probably translated directly from Objective-C, which uses similar syntax for singletons.

Also, with Swift 1.0 we weren't very sure how to create singletons and whether static let on a class is Thread safe.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure the context. But let me explain one of the key differences between

// 1
class var sharedInstance: WebServer {
    return WebServerSharedInstance
}

vs

// 2
static let sharedInstance = WebServer()

1 is computed variable which gets evaluated each time you access, while 2 get initialized lazily and evaluated only once.

If later in your code someone reassigns the WebServerSharedInstance

The 1 will return the new value while 2 will keep returning the initial value if it got initialized before.

3 Comments

You don't know what a computed variable is !!! a variable can be computed. This means that the variable, instead of having a value, has functions. One function, the setter, is called when the variable is assigned to. The other function, the getter, is called when the variable is referred to.
Not sure what you mean. But for reference. You can simplify the declaration of a read-only computed property by removing the get keyword and its braces
sorry you right. didnt know that you can remove the keyword
0

A suggestion is that WebServerSharedInstance is a global variable declared at the top level of the file which lives as long as the file lives which is as long as the program runs --> You can use that variable in all other files

4 Comments

Why isn't WebServerSharedInstance a global variable?
WebServerSharedInstance is a global variable which you can use in all other files of your program.
Then why would you need the second variable?
You are copying the value of your global variable to do some other calculation and keep the value of your global variable clean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.