I have a global variable that needs to be shared among my ViewControllers.
我有一个全局变量,需要在我的ViewControllers之间共享。
In Objective-C, I can define a static variable, but I can't find a way to define a global variable in Swift.
在Objective-C中,我可以定义一个静态变量,但是我找不到一种方法来定义Swift中的全局变量。
Do you know of a way to do it?
你知道怎么做吗?
2 个解决方案
#1
149
From 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"
and you can access from anywhere in current module.
您可以从当前模块的任何位置访问。
However you should avoid that 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
如swift中的答案所示,您可以将它们封装在struct中,并可以访问任何地方。您也可以在swift中定义静态变量或常量。的封装结构
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"
#2
13
Global variables that are defined outside of any method or closure can be scope restricted by using the private keyword.
在任何方法或闭包之外定义的全局变量可以使用private关键字来限制范围。
import UIKit
// MARK: Local Constants
private let changeSegueId = "MasterToChange"
private let bookSegueId = "MasterToBook"
#1
149
From 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"
and you can access from anywhere in current module.
您可以从当前模块的任何位置访问。
However you should avoid that 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
如swift中的答案所示,您可以将它们封装在struct中,并可以访问任何地方。您也可以在swift中定义静态变量或常量。的封装结构
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"
#2
13
Global variables that are defined outside of any method or closure can be scope restricted by using the private keyword.
在任何方法或闭包之外定义的全局变量可以使用private关键字来限制范围。
import UIKit
// MARK: Local Constants
private let changeSegueId = "MasterToChange"
private let bookSegueId = "MasterToBook"