Swift Learning Notes (ii)--Constants and variables

Source: Internet
Author: User

This blog will learn about the constant constants and variable variable in swift. This is the basis of language learning. It can be seen that swift every sentence behind the basic is not, semicolon, if there is plus, the habit of semicolons can also be added.

(1) Constant declaration: Swift will use the keyword let to declare a constant. After you define a constant, you can no longer modify it, or you will get an error.

Let Maxnum = 100

Maxnum = 200//here will be an error, the constant value can not be modified;


(2) Variable declaration: Swift will use the keyword var to declare a variable, and the value of the variable can be changed.

var minnum = 1

Minnum = 2//can modify the value of a variable


(3) Multiple constants and variables can be declared in the same row, separated by commas, as in C.

var x=1.0,y=2.0,z=3.0


(4) Swift is a type-safe language. Variables defined by Var are checked for type.

var maxnum = 100//At this time the default maxnum is an integral type

Maxnum = "Hello"//error, cannot assign string to integer type


(5) A variable can also specify a type directly at the time of declaration. The string is specified as follows.

var myname:string

MyName = "Jack"


(6) You can also declare multiple variables of the specified type on the same line.

var red,blue,green:int

Red=1

blue=2

Green=3


(7) Basic data type: INT, double,float,string

The integral type of the other has int8,int16,int32, in general programming int is enough. You can refer to the official Swift documentation for specific differences.


Double is a 32-bit floating-point number, which can be exactly 15 digits after the decimal point;

Float is a 16-bit floating-point number, which can be exactly 6 digits behind the decimal point;


Strings are string types and are enclosed in "double quotation marks", as compared to Java. With OC to differentiate, prefixes do not require the @ symbol.


(8) Swift also supports the use of a different binary to define the int type, which can be used in binary, octal, decimal, hexadecimal.

Let decimalint:int = 17//decimal denotes the integer type, directly write the number on the line to Binaryint:int = 0b10001//binary represents an integral type, the number to begin with 0b let Octalint:int = 0o21//octal denotes an integer type, The numbers have to begin with a 0o let hexadecimalint:int = 0x11//hexadecimal denotes an integral type, the number starts with 0x

The output in Swift is as follows: The number represented is 17 under decimal.



(9) Swift can also use scientific notation to represent floating-point numbers.

Let Float_normal = 0.012let float_science = 1.2e-2

The output is as follows: In line with our expectations.



(10) It is also possible to specify a floating-point type when using scientific notation.

Let normal:double = 0.012let science:double = 1.2e-2

The output is as shown above.


(11) Convenient means of large number, the middle of using _ underline separated. Make it look more concise.

Let Bignuma = 100000000//Original Presentation method
Let Bignumb = 100_000_000//The representation method that the foreigner likes
Let BIGNUMC = 1_0000_0000//Chinese Express method of liking

Output: In line with our expectations.


(12) Of course, the integral type can also be expressed in scientific notation.

Let Intnormal = 1000let intscience = 1.0e3

The output results are as follows:



(13) Type conversion: Swift does not support different types of reciprocal operations. such as integer and floating-point add-on will be error.

Let A:int = 3
Let b:double = 0.1415926
Let pi:double = a + b//will error here;

The error is as follows:



You need to use coercion type conversions here. The third line of code changes to:

Let pi:double = Double (a) + B;

The output of the modified result is as follows: it meets our expectations. Indicates that arithmetic operations can be performed only under the same type.



Swift is Unicode-encoded, and variables in Swift can be used in Chinese.

As shown below:



summed up, constants and variables this content is also more content, we do not need to memorize, in the future development of the project slowly mastered on it can be.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift Learning Notes (ii)--Constants and variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.