Global Variables in Swift

Stop! Stop! Stop! WRONG. BAD.

Photo by Fernando @cferdo on Unsplash

Prerequisites:

The example

The case for: Globals are a fast, easy to use technique

Make small programs easy!

Somethings are available everywhere

Wrapping globals as Singletons isn’t the way to go

Globals can save keystrokes

The thought problem

The case against: Globals are evil

Thread safety

Coupling and Testing

Even for small programs, it’s worth doing things right

Alternatives

Passing data along view controllers

The Singleton option

Class SingletonClass {
static let sharedInstance = SingletonClass
private init() {}
}

Conclusion

The Twitter contact:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store