swift 全局变量_全局变量Swift

本文介绍了Swift编程语言中的全局变量概念,包括其定义、用法和潜在的影响。通过翻译自Steven P. Curtis的文章,读者将深入理解如何在Swift项目中正确使用全局变量。
摘要由CSDN通过智能技术生成

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…

本文是关于在正确的时间做出正确的选择的…

先决条件: (Prerequisites:)

  • Coding in Swift Playgrounds (guide HERE) or create a single view application in Xcode (guide HERE)

    在Swift Playgrounds中编码(指南HERE )或在Xcode中创建单视图应用程序(指南HERE )

这个例子 (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 ),然后可以从您应用程序中的任何位置(可能是用户按下按钮时)进行更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值