ios 在 Swift 中声明全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26601163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring Global Variables in Swift
提问by Blake Morgan
I want to make a global array of custom objects that can be accessed throughout the app (AppDelegate, ViewController classes, TableViewController classes, etc). I have researched for a way to do it, but have not found an answer. I have tried making giving the array a public scope, but I get a complier warning which says Declaring public variable from internal class
and when I try to access it in a different file, I get an error that says Use of unresolved identifier 'arrayObjectives'
我想创建一个可以在整个应用程序中访问的自定义对象的全局数组(AppDelegate、ViewController 类、TableViewController 类等)。我已经研究了一种方法来做到这一点,但还没有找到答案。我曾尝试让数组成为公共范围,但我收到一个编译器警告,Declaring public variable from internal class
当我尝试在不同的文件中访问它时,我收到一个错误消息Use of unresolved identifier 'arrayObjectives'
How would I go about making that array globally accessible to all files in the application and where would I instantiate that array?
我将如何让应用程序中的所有文件全局访问该数组,以及我将在哪里实例化该数组?
采纳答案by Paulw11
From the Swift Programming Language-
来自Swift 编程语言-
Global variables are variables that are defined outside of any function, method, closure, or type context
全局变量是在任何函数、方法、闭包或类型上下文之外定义的变量
So you can simply declare your variable at the top of any file straight after the import
statements.
因此,您可以直接在import
语句之后直接在任何文件的顶部声明您的变量。
However, I would suggest you seriously reconsider. Generally globals aren't a good idea. You are better off with properties on a singleton or using dependency injection.
但是,我建议您认真重新考虑。通常全局变量不是一个好主意。您最好使用单例属性或使用依赖注入。
Your second question "where would I instantiate the array?" is part of the reason why globals are bad - their lifecycle isn't well defined in terms of your other objects. A singleton that is initialised on first use eliminates this issue.
你的第二个问题“我会在哪里实例化数组?” 是全局变量不好的部分原因——它们的生命周期在你的其他对象方面没有很好地定义。首次使用时初始化的单例消除了这个问题。
回答by Dharmesh Kheni
You can set global Array like this way :
您可以像这样设置全局数组:
import UIKit
var abc : String = String()
and you can access it in any other file like :
您可以在任何其他文件中访问它,例如:
abc = "ABC"
回答by sean
Try making a new Swift file with this:
尝试用这个创建一个新的 Swift 文件:
struct Constants {
static let appName: String = "My App"
struct Colors {
static let colorTextStandard = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.3) //#000000
}
struct Data {
static var myStrings = [String]() // Yea, this is not a constant, but that's alright...
}
}
You can then refer to those global constants (or you can make them variables) using:
然后,您可以使用以下方法引用这些全局常量(或者您可以将它们设为变量):
Constants.appName
or
或者
Constants.Colors.colorTextStandard
or
或者
Constants.Data.myStrings = [stringOne, stringTwo]
回答by anthonyliao
This is how I did it...
我就是这样做的...
class MessageViewCell {
struct MessageViewCellHeightCache {
static var cache: [String:CGFloat] = Dictionary<String, CGFloat>()
}
}
And I accessed it as follows:
我按如下方式访问它:
MessageViewCell.MessageViewCellHeightCache.cache["first"] = 12.0