Quick Start (1)-basic syntax and basic syntax

Source: Internet
Author: User

Quick Start (1)-basic syntax and basic syntax

I recently started to learn swift and organized the learning process and summary into a series to facilitate future review and summarization.

Basic syntax

No extra points are required for each row in swift. Separate multiple statements in the same row
// Indicates the comment, or use /*...... */

Constant

A constant is the amount of data that cannot be modified after definition. For example, if you set a maximum number of login attempts, its value should not be modified in the program once it is determined. Constants in swift are represented by let. The definition method is as follows:

Let maxAttemptTime = 3 // correct maxAttemptTime = 4 // error. The constant cannot be changed once defined. let maxAttemptTime // Error

In addition, it should be noted that the constant must be assigned an initial value during definition. Of course, this can be avoided, which will be detailed later.

Variable

Unlike constant classes, variables are represented by var and can be changed at will.

Var currentAttemptTime = 1 // correct currentAttemptTime = currentAttemptTime + 1 // correct
Print constants and variables

The NSLog method in OC is still available. You can also use the swift println () method.

NSLog ("hello world") println ("hello world") // The two sentences are equivalent, the output result is "hello world" // You can also print the variable or constant var name = "kt" NSLog ("name = \ (name )") println ("name = \ (name)") // the above two sentences are equivalent, and the output results are all "name = kt"

Swift also supports the print method. Unlike println, println automatically adds line breaks at the end, which is consistent with java syntax.

Type Derivation

Programmers who are used to oc, c ++, or java may think that the type without variables is very strange. In fact, swift and php are similar to js and support the type derivation function.

The so-called type deduction means that swift will automatically export the type of the variable based on the actual value.

The basic types in swift include Int, Double, String, UInt8, Character, and Bool. I will not explain them one by one. All types in Swift are capitalized.

Var name = "kt" // automatically derives to String type var age = 20 // automatically derives to Int type var pi = 3.1415926 // automatically derives to Double type
Width type first

Since swift supports type derivation, the approximate value of π is 3.1415926. Why is the type deduced? Is it Float or Double? The answer is Double, because swift has a wide type priority, that is, to automatically derive a type with a wider value range.

Type Annotation

The initial value must be assigned to a constant or variable. If you do not want to assign an initial value or are not used to automatic type derivation, you can also specify the type of a constant or variable, this is called "type annotation"

Var name: String // Add a colon and a type name after the variable name for Type annotation // after the type annotation, the initial value var name can be not assigned: string = "kt" // manually labeled as String type var age: Int = 20 // manually labeled as Int type // In this case, the Type annotation is somewhat redundant.
Type Security

Swift supports type derivation, which is convenient and rigorous because it is type-safe.

The so-called type security means that implicit type conversion between constants and variables is not supported.

Var a = 1var B = 1.5var c = a + B // Error

That is to say, different types of variables (constants) cannot appear on both sides of the "+,-, *,/, and other operators ".
The so-called between constants and variables actually means implicit type conversion between literal volumes. For example, the following code:

Var pi = 3 + 0.1415926 // This is required. Otherwise, it will be messy.
Type conversion

What if I want to add the integer and floating point variables? I can use forced type conversion, also known as explicit type conversion.

Var a = 1var B = 1.5var c = a + B // error var c = Double (a) + B // correct, the Double variable cvar c = a + Int (B) with a value of 2.5 is also obtained. // The Int variable c with a value of 2 is also obtained.

Three points need to be noted:

Type alias

The typelias keyword can be used to rename the type, which is similar to typedef in C language, but is simpler.

Typealias Name = String // now the Name type and String type are exactly the same. Var myName: Name = "kt"
Appendix Swift getting started series tutorials Swift getting started (II) -- getting started with character and string Swift (iii) -- Tuple Swift getting started (iv) -- Optionals and Assert)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.