Do variables in AppDelegate put a workaround for a static variable in Swift?

Since Swift has no static variables, and I'm trying to access a connection, which should be static ... puts this variable in the App Delegate with a reasonable solution?

I found this snippet on GitHub :

func xmppStream () -> XMPPStream {
    return appDelegate().xmppStream!
}

So, when xmppStream()called in code, does it return the original instance or what is actually done here?

0
source share
1 answer

This is not entirely accurate. Swift CLASSES do not have static variables, but structures and enumerations do!

struct Static {
    static var stream: XMPPStream?
}

And you can initialize it later in your code if you want.

Static.stream = XMPPStream()

, , , :

class RegularClass {

    struct Static {
        static var stream: XMPPStream?
    }

    //Other code
}

, ...

RegularClass.Static.stream ...

, , Swift. .

+3

All Articles