- Mastering Swift
- Jon Hoffman
- 267字
- 2021-07-16 14:12:11
Constants and variables
Constants and variables associate an identifier (such as myName
or currentTemperature
) with a value of a particular type (such as a String
or Int
) where the identifier can be used to retrieve the value. The difference between a constant and a variable is that a variable can be updated/changed while a constant cannot be.
Constants are good for defining values that you know will never change, such as the freezing temperature of water or the speed of light. Constants are also good for defining a value that we use many times throughout our application, such as a standard font size or maximum characters in a buffer. There will be numerous examples of constants throughout this book.
Variables are more common in software development than constants. You can make useful applications without using constants (although it is a good practice to use constants); however, it is almost impossible to create a useful application without variables.
You can use almost any character in the identifier of a variable or constant (even Unicode characters); however, there are a few rules that you must follow, they are as follows:
- An identifier must not contain any whitespace
- An identifier must not contain any mathematical symbols
- An identifier must not contain any arrows
- An identifier must not contain private use or invalid Unicode characters
- An identifier must not contain line or box drawing characters
- An identifier must not start with a number, but they can contain numbers
- If you use a Swift keyword as an identifier, surround it with back ticks
Note
You should avoid using Swift keywords as identifiers.
- Web程序設計及應用
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- WildFly:New Features
- JavaScript+jQuery網頁特效設計任務驅動教程(第2版)
- MySQL 8 DBA基礎教程
- Koa開發:入門、進階與實戰
- 老“碼”識途
- ASP.NET 3.5程序設計與項目實踐
- Python入門很輕松(微課超值版)
- Java程序設計與項目案例教程
- Arduino Wearable Projects
- 計算機應用基礎(第二版)
- SQL Server 2008實用教程(第3版)
- 一覽眾山小:ASP.NET Web開發修行實錄
- H5頁面設計與制作(全彩慕課版·第2版)