2. Swift getting started tutorial (1) (complete)

Source: Internet
Author: User

Generally, when we learn a new language, we write a Hello World applet. In swift, we can implement it through a line of content.

Println ("Hello, world") // Note: Output HelloWorld

If you have written C and OC code, this statement may be very familiar to you. In swift, such a line of code is a complete program, you do not need to import another function library for it, such as input/output or string handle. Code written in the global scope will be used as the entry to the program, so you do not need a main function, and you do not need to write a semicolon at the end of each statement.

This tutorial uses a series of small programs to help you use swift to write programs. Do not worry if you cannot understand some of the content, because this book will be explained in more detail at the end.

Prompt
Based on experience, you 'd better open playground in xcode to view the code in this chapter.
Playground allows you to edit the code and immediately see the effect

 

Value Type

Let keywords are used to define constants and var keywords are used to define variables. The value of a constant can be uncertain during compilation, but you can only assign it a value once: you can assign a variable to a constant. You do not know the value of the variable during compilation, but you can only repeat the constant once and define it ). This means that you can name a value and use it in many places)

Var myVariable = 42

MyVariable = 50

Let myConstant = 42

Note: A variable myVariable is defined. The initial value is 42, and the value is changed.
The third sentence defines a constant with a value of 42.

The value of a variable and a constant must be of the same type as your value, but you do not need to define the type clearly. When you create a value for a variable and a constant, the compiler automatically defines its type, in the preceding example, the compiler finds that myVariable is an integer because its initial value is an integer. (Translator's note: you can write a small example to set the initial value of a variable to an integer type and then assign the String value. An error is returned, indicating that the type cannot be converted)

If the initialization value does not provide sufficient information or is more intended to provide an initial value, you can use a variable or constant name following the colon and type to define the type, if there is no initial value, or the initial value is different from the type you want, you must provide a type. This compiler automatically matches the type, which is an occurrence point of project instability, we recommend that you define the type)

Let implicitInteger = 70
Let implicitDouble = 70.0
Let explicitDouble: Double = 70
Note: The third type is to forcibly define the double type.

 

Test

Try to create a constant that is definitely defined as Float type, and then input a value of 4

 

The value cannot be implicitly converted to another type, and the type to be converted is specified.

 

Let label = "The width is"
Let width = 94
Let widthLabel = label + String (width)

Note: width is Int type, label is String type, and width is converted to String type through String (), and is combined with label and assigned to widthLabel.

 

Test

Try to remove the String () above to see what will happen (Translator's answer: Operator + overload failure)

 

There is also a simpler way to convert a value to a String, through "\(...)" This method, for example:

 

Let apples = 3
Let oranges = 5
Let appleSummary = "I have \ (apples) apples ."
Let fruitSummary = "I have \ (apples + oranges) pieces of fruit.

Note: String Conversion operator. Note that this operator can only be used in "".

 

Test

Use the \ () operator to convert a floating point type

 

To create an array or dictionary, you can use the [] Operator and write the element and array serial number or dictionary key value in []. For example:

 

Var shoppingList = ["catfish", "water", "tulips", "blue paint"]
ShoppingList [1] = "bottle of water"

Var occupations = [
"Malcolm": "Captain ",
"Kaylee": "Mechanic ",
]
Occupations ["Jayne"] = "Public Relations

Note: definitions of arrays and dictionaries and elements

 

You can use an initialization statement to create an empty array or dictionary. For example:

 

Let emptyArray = String [] ()
Let emptyDictionary = Dictionary <String, Float> ()

 

If a galaxy of the type can be inferred in subsequent code, you can create an array of unspecified types and a dictionary of unspecified types by using an empty [] or [:], for example:

 

Var shoppingList = []

Var shoppingDict = [:]

Note: Create an empty array and an empty dictionary

 

Part 1

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.