A tentative study of swift programming language

Source: Internet
Author: User
Tags case statement instance method

Following WWDC2014, the new programming language Swift surfaced. It has the characteristics of high speed, modern, safe, interactive and so on, and its syntax is simple, the threshold of entry is low, it is hopeful to replace the objective-c language with complicated grammar. According to its author, Chris Lattner, on the blog, the language has been developed for only 4 years. And the underlying framework is basically developed by one person.

As a rule, use Swift to write a simple "Hello world"

println ("Hello World")

Yes, it's that simple, there is no import statement, no main function, no @ symbol, and semicolons are not required. Compile execution to see the word "Hello World" printed on the console.

Here println is the global function, responsible for printing the parameters and wrapping.

Swift can be said to be the result of merging other good programming languages, so many grammars have something in common with other languages.

Here's a brief look at some of the language's content:

1. Swift declares the variable with Var and declares the constant with let. Such as

var legs = 4               //variable legslet pi = 3.1415926    //Constant PI
So what kind of legs and pi are they? The answer is type inference (genre guessing). Since these two statements are initialized at the same time in the declaration, because 4 is of type int. So guess the variable legs is of type int. Similarly. 3.1415926 is a double type, so guess pi is a double type.

It is also possible to enforce a specified type:

var legs:intlet pi:double
If the legs variable is an integer type, simply add a colon after the variable name, followed by a type name.

2. The assignment operator does not return a value. And in some other languages. The assignment operator returns the value of the variable. (such as a = 5 expression returns 5), and as a novice when making conditional inference, the assignment operator is often used to replace "= =". Causes some very difficult to find logic errors, and in Swift there is no such situation, because this is a syntax error. An error message is given during compilation, such as if a = 5 {...}, if it accepts a bool value and a = 5 has no return value, an error occurs.

/* in C + + */int a = 1;if (A = 5) {...}  Compile success/* in Swift */var a = 1if a = 5 {...}   Compile error//If a = = 5 {...} This is correct one

3. The switch statement does not have a break after the case.

This is also a common mistake for beginners in other languages, and in Swift, the switch-selected case statement jumps out of the switch statement as soon as it runs, rather than continuing to run the following case or default statement because there is no break after the statement.

var a = 1switch a {case 1:println (1) Case 2:println (2) default:println (3)}//No.

4. Ability to use N.. M or n...m instead of an array, for example:

For index in 0: < 3 {println (index)}//0//1//2
.. Represents the front closed and open. ... The delegates are closed before and after, so 0. 3 represents the array [0,1,2], and 0 ... 3 Delegates [0,1,2,3]

5. How do i insert a variable or constant in a string? Swift provides a special syntax:

var number = 5println ("I got \ (number) Books")//I got 5 books
is not very easy. Simply wrap the variable or constant you want to insert in the string (and).

6. In Swift, there are two types, one is the reference type (reference type), one is the value type (value type), and the class is the reference type. While enumeration and structs are value types, one important difference is that they are passed differently, one is the passing copy, and the other is the delivery itself.

In Swift, the class looks like this:

Class Vehicle {    var wheels:int?    var maxpassengers:int?    Func description (), String {        return ' this vehicle have \ (wheels!) Wheels,max passengers is \ (maxpassengers!) "}    Init (withwheels wheels:int,andmaxpassengers maxpassengers:int) {        self.wheels = Wheels        Self.maxpassengers = maxpassengers}    deinit {}}
The class, named vehicle, declares two properties, wheels and Maxpassengers. and an instance method description, which has no parameters and the return value is a string type. At the same time. This class provides a method of initialization. Init is keyword, which accepts two parameters representing the two attributes previously declared. The reference contains three parts, each of which is an external parameter name (external parameter name). Local parameter name and parameter type, the external parameter name is used in the method call, and the local parameter name is used in the method body. The advantage of introducing such a method is that it inherits the good characteristics of objective-c, makes the code segment more like a sentence, and more descriptive narrative. To see how to use this class:

var porsche = Vehicle (withwheels:4,andmaxpassengers:5) println (Porsche.description ())//This Vehicle have 4 Wheels,max Passengers is 5
In addition, in the properties of the class, it represents optional. That is, the property has either a value. Either there is no value (nil).


for Swift's syntax document see: http://download.csdn.net/detail/czjuttsw/7450821





A tentative study of swift programming language

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.