Use playground to write the first swift Program

Source: Internet
Author: User

Outputting "helloworld" from the console is the first step in C language learning and a very important step in my life. Today, many years later, I still hope to use helloworld as the first step to start a magical and magnificent world-Swift programming with everyone.

This chapter uses helloworld as the starting point to introduce how to use playground of xcode to write and run swift program code.

You can write and run swift programs in multiple ways. You can create an iOS or Mac OS X Project in xcode, or use playground provided by xcode6. In the learning stage, we recommend that you use playground to write and run swift programs.

Programming tool Playground

Playground cannot be separated from xcode6, which is a new function added by Apple in xcde6. Using xcode to create a project and write and run a program, the purpose is to compile and release the final program, playground is used to learn, test algorithms, verify ideas, and visualize running results.

As shown in the following figure, the playground program runs. Area ① Is the Code compiling area, area ② is the running result area, and area ③ is the timeline area. The timeline allows you to view the running results of a program from top to bottom by time. The running results of different time stages can be displayed to developers through text, graphics, and curve charts.


Playground Interface

Compile the helloworld Program

The following describes how to use playground to compile the helloworld program. First, open the welcome page of xcode6 (2-2 ). Generally, you can see this interface when xcode6 is started for the first time. If not, you can choose windows> welcome to xcode.

On the welcome page shown in, click "get started with Playground" to bring up the dialog box as shown in. In this dialog box, you can modify the playground file name and directory to be saved. Click "CREATE" to create a playground.

You can edit the template on the Interface shown in. The template has generated some code and the modification code is as follows:

Import cocoa

 

VaR STR = "Hello World"

Println (STR)

After the code is modified, the code will be compiled and run immediately, but we can only see the STR variable on the right side, not the output result of println, as shown in 2-5. Click "value history" next to "Hello World" to open the timeline, as shown in.


The output result is displayed in the timeline. The console output is the output result of the println function.

You can also open the playground timeline interface in the assistant editor. The specific operation process is shown in. Right-click the title bar, select "icon and text" from the menu, and click Open assistant editor in the toolbar that appears. The timeline interface is also available in the assistant editor.

Code explanation

The implementation of helloworld in SWIFT is much easier than that in C, objective-C, and other languages. The code is explained in detail below.

1. Import cocoa statement

Import cocoa indicates the introduction of the cocoa framework, similar to # import in objective-C and # include in C. As to what kind of cocoa framework will be introduced later, we need to find the API to determine. In this example, we do not need the cocoa framework at all, but the import does not matter.

2. var STR = "Hello World"

Declare the STR variable. var indicates the declared variable. VaR does not show the type of the variable, but Swift can infer the type of the variable by assigning values. Given that the value is a "hello World" string, STR is a string variable. We should also note that there is no semicolon (;) at the end of a statement such as C and objective-C (;).

3. println (STR)

Println is a function that can output variables or quantities to the console, similar to the println function in C and the nslog function in objective-C. We will introduce the problem of formatting output later.

In this way, we implement a helloworld output function through just three lines of code. In fact, we can write less.

 


For more information, please refer to the First SWIFT book swift development guide in China.

This book discussion site: http://www.51work6.com/swift.php

Welcome to the swift technology discussion group: 362298485

Welcome to Zhijie IOS public classroom Platform




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.