First, basic grammar

1. Constants and variables

  • Constant (let) : The value of a constant cannot be changed once set.
    1. Its value is not required to be determined at compile time, but must be assigned once before it can be used
    2. You can only assign once
  • Variable (VAR) : The value of a variable can be changed at will

2. Identifiers

  • Identifiers (such as constant names, variable names, function names) can use almost any character
  • The identifier cannot start with a number and cannot contain special characters such as whitespace characters, tabs, and arrows

3. Common data types

  • Value types
    1. Enumeration: Optional
    2. Constructs: Bool, Int, Float, Double, Character, String, Array, Dictionary, Set
  • Reference types
    1. class

4, comments,

5. Literals

6. Type conversion

7, yuan group

Tuples combine multiple values into a single compound value. Values within a tuple can be of any type and are not necessarily of the same type.

Let http404Error = (404, "Not Found") // http404Error is of type (Int, String), Value is (404, "Not Found") let (statusCode, StatusMessage) = http404Error print("The status code is \(statusCode)") // print "The status code is 404 "print("The status Message is \(statusMessage)")  let (justTheStatusCode, _) = http404Error print("The status code is \(justTheStatusCode)") // "The status code is 404 "let http200Status = (statusCode: 200, description: "OK") print("The status code is \(http200statuscode)") // "The status code is 200 "print("The status message is \ [http200Status. The description)) / / output "The status message is OK"Copy the code

Type aliases

A type aliases defines another name for an existing type. You can define type aliases using the TypeAlias keyword.

Typealias AudioSample = UInt16 var maxAmplitudeFound = Audiosample. minCopy the code

9. Boolean values

Swift has a basic Boolean type called Bool. Boolean values refer to logical values because they can only be true or false. Swift has two Boolean constants, true and false

10. Basic operators

1. The assignment operator

  • The assignment operator (a = b) that initializes or updates the value of A with the value of B

2. Arithmetic operators

  • Addition (+) : The addition operator can also be used to concatenate strings
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

3. Combinatorial assignment operators

  • The combinatorial assignment operator var a = 1 a += 2

    // The expression a += 2 is short for a = a + 2

4. Compare operators

  • Is equal to a is equal to b.
  • Does not equal (a! = b)
  • Greater than (a > B)
  • Less than a is less than b.
  • Greater than (a >= b)
  • Less than or equal to (a <= b)
  • Swift also provides both identity (===) and non-identity (! ==) to determine whether two objects reference the same object instance

5. Ternary operators

— — — — — – > the problem? Answer 1: Answer 2

6. Null conjunction operators

  • The null conjunction operator (a?? B) The optional type a is nullified and unpacked if a contains a value, otherwise a default value b is returned. The expression a must be of type Optional. The type of the default value B must be the same as the type of the value a stores.

7. Interval operators

  • The closed interval operator (a… B) Define an interval containing all values from a to B, including both a and b. A cannot exceed B.
  • The half-open interval operator (a..
  • The closed range operator has an alternate expression for an interval that extends indefinitely to one side. Let range =… 5 range.contains(7) // false range.contains(4) // true range.contains(-1) // true

8. Logical operators

  • Logical non (! A)
  • Logic and (a && B)
  • Logic or (| a | b)

9. Identity operators

  1. It is sometimes useful to determine whether two constants or variables refer to the same class instance. To do this, Swift provides two identity operators
    • Same (===)
    • Not the same (! = =)
If tenEighty === alsoTenEighty {print("tenEighty and alsoTenEighty refer to the same VideoMode instance.")} // Print "tenEighty and alsoTenEighty refer to the same VideoMode instance."Copy the code

Notice the difference between “same” (denoted by three equal signs, ===) and “equal” (denoted by two equal signs, ==). “Same” means that constants or variables of two class types refer to the same class instance. “Equal” means that the values of two instances are “equal” or “equivalent,” subject to the designer’s defined criteria.