Introduction to Swift (i)--basic syntax

Source: Internet
Author: User
Tags value of pi

Recently began to learn swift, the process of learning and summarized into a series, easy to review the summary later.

Basic syntax Base syntax

No semicolon is required at the end of each line in Swift, and multiple statements need to be separated within the same line
Indicate a comment, or use the/* ... */

Constant

A constant is the amount of a value that cannot be modified after a definition, such as setting a maximum number of attempts to log on, and once its value is determined, it should not be modified in the program. Constants in Swift are represented by let, and the method is defined as follows:

let3//正确4//错误,常量一旦定义不可以改变let//错误 

It is also important to note that constants must be assigned the initial value at the time of definition, and of course not, and will be described in detail later.

Variable

Unlike constant classes, variables are represented by Var and can be arbitrarily changed.

var1//正确1//正确
Printing constants and variables

The NSLog method in OC is still available and can be used with Swift's println () method.

NSLog("hello world")println("hello world"//以上两句等价,输出结果都是"hello world"//也可以打印变量或者常量var"kt"NSLog("name = \(name)")println("name = \(name)")//以上两句等价,输出结果都是"name = kt"

Swift also supports the Print method, and unlike println, println automatically adds line breaks at the end, which is consistent with the Java syntax.

Type type derivation

Used to OC, C + + or Java program Ape will feel that no variable type is very strange, in fact, Swift and PHP and JS similar, are supported type deduction function.

The so-called type deduction, that is, Swift will automatically derive the type to which the variable belongs, based on the actual value of the variable.

The underlying types in swift are int, Double, String, UInt8, Character, BOOL, and so on. Don't explain it all. All types in Swift are capitalized in the first letter.

var"kt"//自动推导为String类型var20//自动推导为Int类型var3.1415926//自动推导为Double类型
Wide Type Limited

Since Swift supports type deduction, then the approximate value of pi is 3.1415926, which is deduced why type? Is it a float or a double? The answer is double because Swift has a wide type-first feature, that is, a type that is automatically deduced as a broader range of values.

Type callout

At the beginning, it is necessary to assign an initial value to a constant or variable, and if you do not want to assign an initial value, or if you are not accustomed to automatic type deduction, we can also display a constant or variable callout type, which is called a "type callout".

varString//在变量名后加上冒号和类型名来进行类型标注//经过类型标注后,可以不赋初值varString"kt"//人为标注为String类型var20//人为标注为Int类型//这种情况下的类型标注有些多此一举
Type safety

Swift supports type deduction, which is convenient, while Swift is also rigorous because it is type-safe.

The so-called type safety refers to the implicit type-type conversions between constants and variables are not supported.

var1var1.5var//报错

In other words, different types of variables (constants) may not appear on the "+ 、-、 *,/, and so on sides of the operator."
The so-called constants, variables, is actually refers to the literal between the implicit type conversion is possible. For example, the following code:

var30.1415926//这个必须可以啊,否则就乱套了。
Type conversions

What if I want to add integer and floating-point variables, you can use coercion type conversion, also known as explicit type conversion.

var1var1.5var//报错var//正确,得到值为2.5的Double型变量cvar//也正确,得到值为2的Int型变量c

Three points need to be explained:

    1. Accuracy from high to low or from low to high can be, but high precision steering low accuracy will lose the data after the decimal point.
    2. The above code cannot be changed to "var C = Float (a) + B" for type safety + wide type precedence.
    3. Note that the syntax differs from the C language, and that the parentheses in the C language (int) A,swift should be added to the variable.
Type aliases

The Typelias keyword allows you to rename a type, similar to a typedef in C, but is simpler.

String//现在Name类型和String类型是完全一样的了。var"kt"

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

Introduction to Swift (i)--basic syntax

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.