This is the first day of my participation in Gwen Challenge
Constants and variables
- A constant can only be assigned once
- Constant values do not need to be determined at compile time, but must be assigned once before being used
- Constants and variables cannot be used until they are initialized
Declarations of constants and variables
- use
let
The keyword declares constants - use
var
Keyword declaration variable
let a = 10 // Declare a new constant with the name a and assign it to 10
var b = 20 // Declare a variable named b and assign it to 20
Copy the code
- You can declare multiple constants or variables on a single line, separated by commas
var x = 10, y = 20
let a = 30, b = 40
Copy the code
Type annotation for constants and variables
var name: String // Declare a variable of type String and name
name = "jay"
Copy the code
You can define multiple variables of the same type in a single line, separated by commas, and add a type annotation after the last variable name
var x, y, z: Double
Copy the code
Note: In general, type annotations are rarely written. If you assign an initial value to a constant or variable, Swift can infer the type of the constant or variable. In the above example, name is not assigned an initial value, it uses the specified variable type
Naming constants and variables
Constant and variable names can contain almost any character, including Unicode characters
letPI =3.14159
letHello =Hello world.
let🐶 🐶 🐶 🐶 ="dog"
Copy the code
Constant and variable printing
var name:String
name = "jay"
print(name) // jay
print("name is \(name)")// name is jay
Copy the code
Comments (single line, multiple lines, nested)
// Single-line comment
/* This is a multi-line comment */
Copy the code
Unlike multi-line comments in C, multi-line comments in Swift can be nested within other multi-line comments. You can write nested comments by starting a multi-line comment block and then starting a second multi-line comment in the first block. Then close the second block, then the first block
/* This is the beginning of the first multi-line comment /* This is the second nested multi-line comment */This is the end of the first multi-line comment */Copy the code
A semicolon
- Swift doesn’t force you to use a semicolon at the end of every statement
;
let cat = "🐱"
Copy the code
- Swift can also add semicolons as usual
;
let cat = "🐱";
Copy the code
- To write multiple independent statements on the same line, use a semicolon
;
let cat = "🐱"; print(cat)
Copy the code
Type the alias
Give an existing type another name, using the TypeAlias keyword
typealias weight = UInt16 // Weight is defined as an alias for UInt16
var value = weight.min // actually uint16. min
Copy the code
Common data types
type | value | |
---|---|---|
Value types (value type ) |
Enumeration (enum ) |
Optional |
Value types (value type ) |
Structure (struct ) |
Bool ,Int ,Float ,Double ,Character |
Reference type (reference type ) |
Class (class ) |
Integer (Int
)
- Integer type:
Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64
- On 32-bit platforms,
Int
和Int32
Same length. On 64-bit platforms,Int
和Int64
The length of the same - Integer maximum value,
UInt8.max
.UInt8.min
- In general, it is used directly
Int
Can be
let minValue = UInt8.min / / minValue to 0
let maxValue = UInt8.max / / maxValue for 255
Copy the code
Floating point Numbers (Float, Double,
)
Float
32 bits, precision only 7 bits (this type can be used if accuracy is not high)Double
64-bit with at least 16 bits of accuracy (use this type when you need to store very large or high precision floating point numbers)
Numeric literals
let decimalInteger = 17 // A decimal number without a prefix
let binaryInteger = 0b10001 // A binary number prefixed with 0b
let octalInteger = 0o21 // An octal number prefixed with 0o
let hexadecimalInteger = 0x11 // A hexadecimal number prefixed with 0x
Copy the code
Type safety and type inference
Swift is a Type safe language, so it does Type checks when compiling code and flags mismatches as errors. This allows you to find and fix bugs early in development
- When inferring the type of a floating-point number, Swift always selects it
Double
Rather thanFloat
type
let a = 42 //a is inferred to be an Int
let b = 3.14159 //b is inferred to be of type Double
Copy the code
- If both an integer and a floating-point number are present in the expression, it is inferred that
Double
type
let c = 3 + 0.14159 //c is inferred to be of type Double
Copy the code
Numeric type conversion
Constant A is of UInt8 type and constant B is of UInt16 type, they cannot be added directly because they are of different types, so call UInt16(a) to create a new UInt16 number and initialize it with the value of A. Both numbers are now of type UInt16 and can be added together. The type of the target constant C is inferred to be UInt16 because it is the sum of two UInt16 values
let a :UInt8 = 10 // A is of type UInt8
let b :UInt16 = 20 //b is of type UInt16
let c = UInt16(a) + b //c is inferred to be of type UInt16
Copy the code
-
Integer and floating point conversions
- Conversions to integers and floating-point numbers must be explicitly typed
let a = 3 // Type A is inferred to be an Int let b = 0.14159 // type b is inferred to be of type Double //let c = a + b let c = Double(a) + b // the c type is inferred to be of type Double Copy the code
- Literals can be added directly because numeric literals themselves have no explicit type
let c = 3 + 0.14159 // the c type is inferred to be of type Double Copy the code
- Floating point to integer (when initializing a new integer value in this way, the floating point value is truncated)
let a = Int(3.14159) / / 3 let b = Int(-4.7) / / - 4 Copy the code
Boolean value (Bool
)
true
false