1 identifiers
Names named for variables, constants, functions, structs, classes, enumerations, methods, protocols, etc. in a program
You can use any character as a named identifier, but you have the following naming convention:
(1) Case-sensitive
(2) cannot start with a number, but can contain numbers in other places
(3) cannot contain spaces, mathematical symbols, arrows, reserved or illegal Unicode code points, lines and tabs
(4) cannot be a keyword, but you can use the inverse quotation mark (') to enclose the keyword in a way that uses it as a name,
You should avoid using keywords as identifiers
var tmpint = 1var Tmpint = 2var tmpstring = "Hello world!" var tmpstring = "Hi world!" var 5tmpInt = 2//errorvar tmp5int = 3//var var = 8//errorvar ' var ' = 8print (' var ')//8\n//var tmp string = "abc"//er Ror//var tmp; string = "abc"//error//var tmp, String = "abc"//errorvar tmp:int, String = "abc"//var Tmp-value = ten//error//var Tmp+va Lue =//errorvar Tmpvalue = 10var _tmpvalue = 20var Apple = "var =" Strawberry "
2 Notes
Comments are divided into two types of annotations:
(1) Single-line comment
Double forward slash (//) as the starting tag
This is a single-line comment
(2) Multi-line Comment
The start tag is followed by a single forward slash followed by a model (/*), with the terminating mark as an asterisk and a single forward slash (*/)
/* This is the first line of a multi-line comment
Second line
Line three */
Multi-line annotations can be nested in other multiline annotations
/* This is the first line of a multi-line comment
Second line
/* This is a nested comment */
This is a single-line comment
Line three */
This is a single-line comment/* This is the first line of a multiline comment the second row of the third row *//* This is a multiline comment on the first row of the second row/* This is a nested comment *///This is a single line comment on the third line */
3 semicolon
It is not mandatory to use semicolons at the end of each statement (;), or you can add semicolons as you want.
When writing multiple independent statements in the same line, you must use a semicolon.
var tmpval = 10;//var TmpVal2 = print (tmpval)//errorvar tmpval2 = 20; Print (Tmpval)
4 Variables and constants
Variables and constants must first be declared and initialized before they can be used.
DECLARE: give the variable and constant a name, and specify a definite data type, that is, the type of the value to be stored
Initialization: Assigning an initial value to variables and constants
(1) Variables
Declaring variables with the keyword VAR, syntax format: var variable name: data type [= initial value]
(2) Constants
Use keyword let to declare variables, syntax format: let constant name: data type [= initial value]
The data saved by the variable can be changed several times, and the constants cannot be changed once the data has been saved.
You can declare multiple constants or variables in a single row, separated by commas. Can be of the same data type, but also for different data types.
If you give an initial value when declaring a variable or constant, you do not need to explicitly specify the data type of the variable or constant.
Swift infers the type of the variable or constant based on the initial value.
//age = 30 //errorvar age:int=30age = 35var name = "Tom" //String//var sex // Type annotation missing in patternvar sex:boolsex = truelet constantval:int = 30//constantval = 40 // errorlet constantval2 = "ABC" //String//let constantval3 errorlet constantval3 : stringconstantval3= "Test" var val1 = 1, val2 = 2 , val3 = 3var a1, a2, a3:intvar a11, a12:int, a13: string//a1 = "Test" //error cannot assign value of string to type intvar b1:int, b2:bool, b3:stringvar c1:int = 10, c2 = 30, c3:String = "Test" var d1 = 10, d2 = 20, d3 = "Test"
Swift Learning Notes _ variables and constants