5

I have created a new file ->swift file . called Globals.Swift Then in there I have done :

class Globals {

    static let sharedInstance = Globals()

    init() {
        var max=100
    }

}

In another class(UIViewcontroller) I would like to use it,

Globals.sharedInstance //is going ok

is good, but when i go deep to .max i get the error.

Curnelious
  • 1
  • 16
  • 76
  • 150
  • 2
    Possible duplicate of [Using a dispatch\_once singleton model in Swift](http://stackoverflow.com/questions/24024549/using-a-dispatch-once-singleton-model-in-swift) – Wayne Tanner Mar 15 '16 at 12:58
  • the question is simple, no need to go far, and not a duplicate. i just get it ok till the point where i try to go deeper with the .max – Curnelious Mar 15 '16 at 13:00
  • 2
    You need to declare `max` as an instance variable of the class... – kennytm Mar 15 '16 at 13:01

1 Answers1

9

You can't just have var = xxx in an init. The variable has to be declared at the class top level.

Example of using your singleton:

class Globals {

    static let sharedInstance = Globals()

    var max: Int

    private init() {
        self.max = 100
    }

}

let singleton = Globals.sharedInstance

print(singleton.max) // 100

singleton.max = 42

print(singleton.max) // 42

When you need to use the singleton in another class, you just do this in the other class:

let otherReferenceToTheSameSingleton = Globals.sharedInstance

Update following Martin R and Caleb's comments: I've made the initializer private. It prevents, in other Swift files, the initialization of Globals(), enforcing this class to behave as a singleton by only being able to use Globals.sharedInstance.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • is there a difference between self.max=100 or max=100 in the init() ? – Curnelious Mar 15 '16 at 13:07
  • 1
    This is a shared object, not a singleton. – Caleb Mar 15 '16 at 13:09
  • 3
    A "singleton" is a class for which only a single instance can be created. (In the old pre-ARC times you could achieve that in Objective-C by overriding alloc/retain/release/...) Here you can create another instance by calling `let g2 = Globals()`, so it is not really a singleton. By making the init method private you can only prevent creation outside of the defining source file. –  Apparently "singleton" is often used as synonym for "shared instance", but it isn't. – Martin R Mar 15 '16 at 13:21
  • 3
    @EricD. A singleton is a class that can be instantiated only once. Your `Globals` class can be instantiated many times, and it is therefore not a singleton even if the class itself provides access to a shared instance. You could solve the problem by making `init()` private to deny access to `Global()` from outside the class. [Here's a thorough treatment of the possibilities for singletons in Swift.](http://krakendev.io/blog/the-right-way-to-write-a-singleton) – Caleb Mar 15 '16 at 13:22
  • @Caleb Many of examples from the link you've posted can be instantiated multiple times (you can set breakpoint to see the memory addresses of each instance)... Can you elaborate how those examples from the link differ from what EricD have posted? – Whirlwind Mar 15 '16 at 14:55
  • @MartinR So, what would be the pure Singleton implementation in Swift then ? (I understand the difference you pointed about shared instance vs Singleton). – Whirlwind Mar 15 '16 at 14:58
  • @Whirlwind I agree that many of the examples are suboptimal or just wrong, but the article does lay out the requirements for being a singleton at the beginning, and if you read through to the end you'll find the optimal implementation. I see that Eric D. has changed his example to use a `private init()`, so this answer now provides a proper singleton. – Caleb Mar 15 '16 at 15:17
  • @Caleb I see. I thought that you posted that link as an example of singleton class that can be instantiated only once. I read that example earlier, and if I remember correctly the `sharedInstance` property is a singleton object, not the class which holds it. That class which holds the `sharedInstance` property can be instantiated multiple times ( to have multiple instances of a class with different memory addresses). – Whirlwind Mar 15 '16 at 15:28
  • @Whirlwind A singleton, by [definition](https://en.wikipedia.org/wiki/Singleton_pattern), is a class that can be instantiated only once. It doesn't make sense to call the shared instance a singleton because the shared instance is, like *every* object, the result of a particular instantiation. The essential feature of a *singleton* is that you can't make two of them. Contrast with a shared object, which is a particular instantiation that happens to be accessible to multiple clients. – Caleb Mar 15 '16 at 15:35
  • @EricD Yeah, as long as the init is private and as long you keep the implementation of a singleton in a separate file from where you actually use it. I just tried to use them inside the same file where they are defined. – Whirlwind Mar 15 '16 at 15:40
  • @Caleb `It doesn't make sense to call the shared instance a singleton because the shared instance is, like every object, the result of a particular instantiation.` You got the point, my bad. I was creating instances in a same file, so private init had no effect. – Whirlwind Mar 15 '16 at 15:41
  • Still, from that link, if you look at second example (aka struct way), the multiple instantiation is possible if you don't use the Singleton as it should (through sharedInstance). That was the another source of this confusion, because I thought that @Caleb implying that examples from there are the examples of classes which can be instantiated only once. But adding the private init can solve that case too. – Whirlwind Mar 15 '16 at 15:51