swift 全局变量
Difficulty: Beginner | Easy | Normal | Challenging
难度:初学者| 容易 | 普通| 具有挑战性的
Global variables are defined outside of functions, methods, closures or types. That is, they are defined at the global scope.
全局变量是在函数,方法,闭包或类型之外定义的。 也就是说,它们是在全局范围内定义的。
Local variables are variables that are defined within a function, method or closure.
局部变量是在函数,方法或闭包内定义的变量。
So why are there global variables when people on the Internet say that they should never be used????
那么当互联网上的人们说永远不要使用它们时,为什么会有全局变量呢?
We need to think about the alternatives that they recommend (if they even do), and make the best choices for our own situation.
我们需要考虑他们推荐的替代方案(如果愿意的话),并针对我们自己的情况做出最佳选择。
This article is about making the right choices at the right time…
本文是关于在正确的时间做出正确的选择的…
这个例子 (The example)
There is a counter variable in your app (perhaps in your AppDelegate
) which can then be updated from anywhere in your App (perhaps when a user presses a button).
您的应用程序中有一个计数器变量(可能在AppDelegate
),然后可以从您应用程序中的任何位置(可能是用户按下按钮时)进行更新。