官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 临泉县| 天津市| 新郑市| 濮阳县| 介休市| 宜州市| 黑山县| 博客| 商洛市| 银川市| 巴林左旗| 神木县| 岑巩县| 昔阳县| 江西省| 泸西县| 大理市| 江川县| 奉新县| 上蔡县| 治多县| 大足县| 濮阳市| 冷水江市| 岳阳县| 朝阳区| 邹平县| 襄汾县| 安福县| 长垣县| 淳化县| 车致| 抚松县| 建瓯市| 弥勒县| 大田县| 长沙县| 黄浦区| 连南| 易门县| 衡南县|