Swift up and running--variables and constants for the park Swift series

Source: Internet
Author: User

Swift up and running--variables and constants

The original study of the park

Parking Learning Techniques Video

When we learn a language, whether it is written or spoken, or whether it is a foreign language or programming language, we will use the elements of the language we have mastered to understand the corresponding parts of the new language. Learning Swift , too, is a programming language that has common concepts and similar expressions with objective-c,JavaScript , and other programming languages such as C # . If you know more or less about some programming languages, learning Swift will be a very easy process.

Playground

Playground is a gadget that Apple released with XCode to debug Swift code in real time in 2014 when WWDC released Swift . Most of the videos we speak about the Swift language itself will be done directly in the Playground . Playground will be as we write code to compile for us in real time, and automatically show us the execution results of each line of code, whether it is a simple statement, a loop, or even a variety of UI,Playground can be perfectly competent, we understand A powerful tool for all language features in Swift .

We open XCodeand choose Get started with a playground.

After that, platform chose iOS to write a name for his Playground , such as the variables-and-constantswe used.

After clicking the Next button to set a save directory for the Playground file, you can start writing code in Playground .

In the we can see that thePlayground to the left is the area where we write the code, and the right side shows the execution result of the corresponding statement.

Start by defining a variable

Variables are the most commonly used syntax elements in programming languages, and they are used to put a name (for example:swiftstring) and a certain type of value (for example:"Swift is fun." ) in a program. ) associated.

In Swift , we use the var keyword to define variables of various types:

var Name = Value

For example, define some basic type variables that we are familiar with:

: # # # Fundamental types//: Integervar hours = 24//: Doublevar PI = 3.14//: Boolvar swiftisfun = true//: Stringvar boxue = "Boxue.io"

  

In addition to these basic types,Swift has some of its own unique types, such as: it is convenient for us to package multiple types of Tuple

: Tuple-pack Many types of values togethervar me = ("Mars", One, "[email protected]") me.0me.1

  

After defining the good one tuple , we can use the sample code,me.0,me.1 to access the members of the tuple in such a way. We can also directly modify the value of a variable as follows:

Hours = 12PI = 3.14159

  

If we don't want the value of the variable to be modified, we need to define a special variable.

Constants are variables that cannot be modified

The methods and variables that define constants are similar, and we can turn the keyword varinto a keyword let. For example:

Let minutes = 30let Fireishot = True

  

Once we have defined constants, we can no longer modify the values of the constants, otherwise the compiler will tell us that an error has occurred. For example, we modify the constant fireishot:

Fireishot = False

  

At this point, we can see a compilation error directly in the Playground :

"Whenever the value of a variable in a program is not modified, you should always use let to define it." This will not only prevent variables from being accidentally modified, but also allow the compiler to do more optimizations for constants. ”

Best Practices
Type inferende/annotation

In the previous example, we can see that whether it is an integer , a floating-point number, a string , or a Tuple, we do not explicitly specify the type of variable, Swift The compiler automatically infers the type of the variable based on our assignment to the variable, which is called type inferencein Swift .

But sometimes, when we define variables, we cannot determine the initial values of the variables, and at this point we have to tell the compiler exactly what type of variables we need, as follows:

var Name : Type

This pass : type Specifies the form of the variable type, called type annotationin Swift . For example:

var X:intvar s:string

  

Using annotations in your code

Swift and C as well as objective-c , support the use of // start of single-line comments, as well as the /**/ surrounded by a multiline comment, included in the code in the comments, The Swift compiler does not process. For example:

: Single line comment/*:a multiple line Commenta multiple line Commenta multiple line comment*/

  

Also, in Playground , we can add some markdown tags to the annotations, so that Playground can help us render similar effects:

Everyone can come to see the full markdown content, here, some of the things we often use.

The method of using Markdown is simple, we add a colon after the start/ * of a single line comment // or multiline comment :, like this ////// :. After that, we click on the Playground menu:editor > Show Rendered Markup to see the rendered effect.

Below, are some of our more commonly used markdown tags:

    • Use #, # ## # # # # # # # # # # # # # # # # # #

Here, the beginning of the # must have a space with the title at the back.

/*:# Title level 1## Title level 2### Title level 3#### Title level 4*/

  

    • Italic or Bold font

We use a pair of asterisks to represent italics, and use a pair of double stars to indicate bold:

: A *bold* or **italic** font.

  

Use * or + to indicate unordered lists:

Here, each item that starts with + or * must have a space with the following item.

/*: + unordered list item 1 + unordered list item 2 + unordered list Item 3 */

  

    • Use 1, 2, 3, 4 to indicate a sequence table:
      /*: 1. Ordered list item 1 2. Ordered list item 2 3. Ordered list Item 3 */

        

    • To represent delimiters with----:

Here, there is a blank line between the contents of the delimiter and the delimiter.

/*: This line is not being rendered. The above line----the bottom line*/

  

Swift up and running--variables and constants for the park Swift series

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.