How to create a global variable?

SwiftGlobal Variables

Swift Problem Overview


I have a global variable that needs to be shared among my ViewControllers.

In Objective-C, I can define a static variable, but I can't find a way to define a global variable in Swift.

Do you know of a way to do it?

Swift Solutions


Solution 1 - Swift

From the official Swift programming guide:

> Global variables are variables that are defined outside of any > function, method, closure, or type context. Global constants and > variables are always computed lazily.

You can define it in any file and can access it in current module anywhere. So you can define it somewhere in the file outside of any scope. There is no need for static and all global variables are computed lazily.

 var yourVariable = "someString"

You can access this from anywhere in the current module.

However you should avoid this as Global variables are not good for application state and mainly reason of bugs.

As shown in this answer, in Swift you can encapsulate them in struct and can access anywhere. You can define static variables or constant in Swift also. Encapsulate in struct

struct MyVariables {
    static var yourVariable = "someString"
}

You can use this variable in any class or anywhere

let string = MyVariables.yourVariable
println("Global variable:\(string)")

//Changing value of it
MyVariables.yourVariable = "anotherString"

Solution 2 - Swift

Global variables that are defined outside of any method or closure can be scope restricted by using the private keyword.

import UIKit

// MARK: Local Constants

private let changeSegueId = "MasterToChange"
private let bookSegueId   = "MasterToBook"

Solution 3 - Swift

if you want to use it in all of your classes you can use:

public var yourVariable = "something"

if you want to use just in one class you can use :

var yourVariable = "something"

Solution 4 - Swift

If you don't actually want a global variable, but instead want to save values that persist even when the app closes you can do this:

If you don't actually want to pass data between view controllers but rather simply want to store a global variable you can do this:

This gives a great explanation for how to do this in Swift 5: https://www.hackingwithswift.com/example-code/system/how-to-save-user-settings-using-userdefaults

Summary:

To set a value:

let defaults = UserDefaults.standard
defaults.set("value", forKey: "key")

To get a String value:

let key = defaults.object(forKey: "StringKey") as? [String] ?? [String]()

To get integer value:

let key = defaults.integer(forKey: "IntegerKey")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionczzhengkwView Question on Stackoverflow
Solution 1 - SwiftcodesterView Answer on Stackoverflow
Solution 2 - SwiftDan CoughlinView Answer on Stackoverflow
Solution 3 - Swiftuser9050369View Answer on Stackoverflow
Solution 4 - SwiftJeffrey KozikView Answer on Stackoverflow