[swift] How to create a global variable?

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?

This question is related to swift global-variables

The answer is


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"

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"