Home>

constantConstants are fixed values ​​that a program cannot change during its execution. Constants can be anything like integer constants,Floating-point constants,The basic data type of a character constant or string.It can also be an enumeration constant.

These constants are treated the same as regular variables,However, their values ​​cannot be modified after they are defined.

Declare constants

When using constants,You must use the let keyword to declare them as follows:

let constantname =<initial value>

Here is a simple example to show how to declare a constant in swift:

import cocoa
</p>
<p>
let consta=44
println (consta)

When we run the above program in the playground, we get the following results.

</p>
<p>
44
</p>
<p>

Type annotation

When declaring a constant,Can provide a type annotation,To specify which constant value type to store.The syntax is as follows:

var constantname:<data type>=<optional initial value>

Here is a simple example to show how to use annotations to declare a constant in swift. It should be noted here that it is mandatory,It is important to provide an initial value when creating a constant:

import cocoa
</p>
<p>
let consta=42
println (consta)
</p>
<p>
let constb:float=3.14159
</p>
<p>
println (constb)

When we run the above program in the playground, we get the following results.

42
3.1415901184082

Named constant

The name of a constant can consist of letters,Numbers and underscores.It must be lettered or underlined. Uppercase and lowercase letters are different,Because swift is a case-sensitive programming language.

Variables can be named using simple or unicode characters.The following are valid examples:

import cocoa
</p>
<p>
let _const="hello, swift!"
println (_const)
</p>
<p>
let variable="hello world"
println (variable)
</p>
<p>

When we run the above program in the playground,A Chinese variable name is printed here:"Variable" and we get the following result.

hello, swift!
Hello world

Print constant

You can use the println function to print the current value of a constant or variable.You can pass the package name in parentheses,And use a backslash before the left parenthesis to escape and interpolate a variable value:The following are valid examples:

import cocoa
</p>
<p>
let consta="valuea"
let constb=1101.00
</p>
<p>
println ("value of \ (consta) is more than \ (constb) millions")

When we run the above program in the playground,We get the following results.

value of valuea is more than 1101.0 millions

variableThe variable gives us a name that the program can manipulate to store.Each variable in swift has a specific type,It determines the size of the variable storage; The range of values ​​stored in memory, and operation groups can be applied to variables.

The following are the basic types of swift variables

int or uint-This is for integers.More specifically, you can use int32, int64 to define 32 or 64-bit signed integers,Among them, uint32 or uint64 are used to define 32 or 64-bit unsigned integer variables. for example, 42 and -23. float-this is used to represent a 32-bit floating point number,Generally used to use smaller decimal numbers. For example:3.14159, 0.1, and -273.158. double-this is used to represent a 64-bit floating point number,Used for very large floating-point values. For example:3.14159, 0.1, and -273.158. bool-this represents a Boolean value,Real or fake. string-This is an ordered collection of characters.For example, "hello, world!" character-This is a single character string.For example, "c" optional-This means that it can hold a value or a variable without any value.

swift also allows defining other types of variables,We will learn about types like optional, array, dictionaries, structures, and classes in the future.

The following sections explain how to declare and use various types of variables in swift programming.

Variable declaration

Variable declaration means telling the compiler how much variable storage space is created.Before using variables,They must be declared with the var keyword,as follows:

var variablename =<initial value>

Here is a simple example to show how to declare a variable in swift:

import cocoa
</p>
<p>
var vara=42
println (vara)

When we run the above program using playground,We get the following results.

42

Type annotation

When declaring a variable,Can provide a type annotation,To make clear what kind of variable value can be stored.Here is the syntax:

var variablename:<data type>=<optional initial value>

Here is a simple example of how swift uses annotations to declare a variable.The thing to note here is thatWithout type annotations,Then it becomes the type of the initial value of the mandatory variable,Otherwise use type annotations to declare the type of the variable.

import cocoa
</p>
<p>
var vara=42
println (vara)
</p>
<p>
var varb:float
</p>
<p>
varb=3.14159
println (varb)

When we run the above program using playground,We get the following results.

42
3.1415901184082

Named variable

A variable name can consist of letters,Numbers and underscores.It must be lettered or underlined.Uppercase and lowercase variables are different,Because swift is a case-sensitive programming language.

Variables can be named using simple or unicode characters.The following are valid examples:

import cocoa
</p>
<p>
var _var="hello, swift!"
println (_var)
</p>
<p>
var variable name="Hello World"
println (variable name)

When we run the above program using playground,We get the following results.

hello, swift!

Hello world

Print variable

Use the println function to print the current value of a constant or variable. You can pass the package name in parentheses,And use a backslash escape before the left parenthesis to interpolate a variable value,The following are valid examples:

import cocoa
</p>
<p>
var vara="valuea"
var varb=1008.00
</p>
<p>
println ("value of \ (vara) is more than \ (varb) millions")

When we run the above program using playground,We get the following results.

value of valuea is more than 1008.0 millions
  • Previous Multiple mouse drag and drop effects with JavaScript
  • Next Regular expression link replacement functions