freeCodeCamp/guide/chinese/swift/variables/index.md

43 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Variables
localeTitle: 变量
---
## 变量
变量将名称与特定类型的值相关联。在Swift中有两种主要的方法来创建变量。 `let`和`var` 。要声明常量,请使用`let`关键字。要声明可变变量,请使用`var`关键字。
Swift中使用两种方式储存变量的好处是防止“改变常量值”这种错误。
```
let daysInAWeek = 7
var amountOfMoney = 100
amountOfMoney = 150 // amountOfMoney现在是150
daysInAWeek = 10 //这给了我们一个错误!
```
在这种情况下,变量`daysInAWeek`应该是常量,因为一周只有七天,而变量`amountOfMoney`应该是var因为一个帐户中的金额变化。
常量和变量名称几乎可以包含任何字符包括Unicode字符
```Swift
let π = 3.14159
let 你好 = "你好世界"
let 🐶🐮 = "dogcow"
```
要测试变量是否具有正确的值,请使用`print()` 。
```Swift
let money = 50
print(money)
// This prints 50
```
#### 更多信息:
* [Swift编程语言](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID310)