Apple new programming language Swift language Advanced (I.)--Overview

Source: Internet
Author: User
Tags case statement

Swift is a new language developed and provided by Apple for the development of iOS and OS X applications. The swift language is based on the C and Objective-c languages, and in addition to providing all the syntactic functions of the C and Objective-c languages, Swift has a lot of syntax optimizations and improvements for programming convenience and efficiency.

Swift employs a secure programming model with many advanced and powerful features such as dynamic runtime, compilation dynamic type checking, Closures, subscript functionality, automatic reference counting, option type, generic type and type compile-time inference, type extension and nesting, protocol type, function and type generalization, Operator customization and addition.

The swift language design idea is: the language and the compiler undertakes the different duty, the language itself undertakes the program semantics the presentation, for the program writing service, for enhances the programming efficiency carries on the maximum degree optimization, but the compiler assumes the language understanding and the compilation responsibility, to the program execution efficiency and the performance optimizes.

As a result, the Swift language is designed to be semantically understandable in order to provide programmatic efficiency in the most grammatical optimization, so that the language itself is more clean and concise, more powerful, more expressive, and without losing flexibility, such as the realization of the language syntax of the object-oriented and procedural parts of the organic unity, Removed the syntax limitations of C language and so on.

Swift Language is optimized by both language and compiler to make programming in swift language easier and more efficient, while program execution efficiency and security are ensured.        

I. The simplicity and flexibility of swift language

Swift language in order to keep the language concise in all aspects of the grammar (including program structure, keywords, etc.) is optimized, on the basis of the understanding of the addition of redundant syntax, can be given to the compiler to work as far as possible to the compiler processing.

As each language implements the first program, print a "Hello, world".

The swift language is just one of the following statements in order to implement the above functionality.

println("Hello, World")

This statement is a complete program in the swift language and does not require the addition of separate libraries and headers, and no common language . Main function declaration, even the last semicolon of the statement is omitted.

The swift language also does not have the usual C-language header files, and all implementations and declarations are in a file that is suffixed with. Swift, which is also needed to improve programming efficiency without requiring users to maintain two files. Interfaces for a class that other code requires are automatically available and used.

The swift language's naming of keywords is also as simple as possible on the basis of accuracy, such as integer type int, not integer, Boolean type bool instead of Boolean.

The Swift language adds a for to the Loop statement - In form, making it easier to traverse arrays, dictionaries, scopes, strings, and other sequences.

For the sake of the cleanliness of the language, the enclosing conditions and the parentheses of the loop variable are omitted.

Such as:

for ( kind numbers ) in interestingnumbers {

    for number in numbers Span style= "letter-spacing:0.0px; Color: #000000 "> {

        if Span style= "letter-spacing:0.0px; Color: #508187 ">number largest {

Largest = number

}

}

}

a simplified syntax for the switch control structure is also provided, for the sake of brevity and fewer errors, the program automatically exits the switch statement after executing the code for each case that corresponds to switch, and no longer executes the following case statement, so you do not need to add a redundant break statement after each case's execution code.

The swift language has been de-redundant in syntax, but has not been streamlined in syntax, but is enhanced to include almost all of the functions of C and Objective-c languages, as well as adding and enhancing many features, such as the following: subscript function, tuple type , option types, extensions and nesting of types, generalization of functions and types, customization and addition of operators, and many more.

In addition, the Swift language is more flexible and has fewer grammatical limitations relative to C or other languages. Such as

annotations in swift language support a single-line comment, starting with//, as in C, and multi-line annotations in/*/form. However, without the C language restriction on multiline annotations, Swift's multi-line annotations support nesting. As shown below:

/* This is the start of the first multiline comment

/* This is the second, nested Multiline Comment */

this is the end of the first Multiline Comment */

    swift also supports printing a variable or constant value using the PRINTLN function or print directly, or inserting a string type for output:


 var   = " hello! "

println (friendlywelcome)

Or

println ("The current value of Friendlywelcomeis \ (friendlywelcome)" ).


In short, the swift language is similar to the C language in grammatical statements, but in many aspects of the syntax of the simplification and enhancement, it can be said that the swift language is the simplification of C language, the function of objective-c language enhancement, and with the expression of scripting language, etc. So swift language is a more natural and expressive language of grammar.


Ii. security and semantic clarity of swift language

Swift is a type-safe language, that is, the type of each variable or constant that must be explicitly used in the compile phase for type checking. Type-safe languages can develop a more secure and reliable program by better discovering and modifying the error of type usage during the development and compilation phase.

Swift does type checking when compiling code, but does not imply that the type must be specified for each declared constant or variable.

The Swift compiler provides a type inference mechanism--when compiling code, automatically infers the type of an expression by simply checking the value of the expression you provide.


Because Swift uses type inference, Swift can provide a more concise type declaration syntax than other languages, although the syntax is concise, but the type can still be clearly determined during the compilation phase. Swift's compiler inference type feature achieves the best possible unification of type safety and syntax simplicity for swift language requirements.

If you use an initial value to declare a constant or variable, the Swift compiler can automatically infer the type of the variable or constant based on the initial value of the variable or constant.

   let = 10

varcurrentloginattempt =0

For the above declarations of maximumnumberofloginattempts constants and currentloginattempt variables, Swift can infer that they are of type int.

Let pi = 3.14159

The above statement Swift infers that you want to create a constant of type double. Swift always infers that a floating-point number is of type double.

If a variable or constant does not provide an initial value at the time of declaration, so that the Swift compiler cannot infer its type, you need to explicitly specify the type for it. Declare a variable of type string, as shown below.

var welcomemessage:string

So Swift is also a language that is more secure and clearer in semantics.

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.