The Swift language Starter Tutorial

Source: Internet
Author: User
Tags arrays constant empty integer naming convention object model

About Swift

Swift is a new programming language for iOS and OS X applications, absorbing the essence of the C and Objective-c languages, but without losing the compatibility with the C language. Swift uses a secure programming model and incorporates a variety of modern programming language features, making the language easier to master, more extensible, and more fun to use. The cornerstone of Swift language is the mature and beloved cocoa and cocoa touch framework, and the new language allows you to enjoy the opportunities for new software development.

Swift has deposited years of research and development, and Apple has laid a solid foundation for efficient swift language compilers, debuggers, and infrastructure. We use automatic Reference counting (ARC) technology to simplify memory management. Our framework design is based on the Cocoa Solid foundation framework. Improvements in standardization and modernization have been thoroughly completed. The improvement of OBJECTIVE-C supports the block definition, the set syntax and the module, which makes the language framework integrate the modern language technology reasonably. Thanks to the basic work of our predecessors, we are now able to introduce Apple's future software development language.

For OBJECTIVE-C developers, Swift is relatively easy to use, using a similar parameter naming convention and a powerful dynamic object model with Objective-c. It provides seamless use of the cocoa framework and the interoperability features that are mixed with objective-c code. Built on these foundations, the swift language brings many new features, as well as the integration of procedural and object-oriented language features.

Swift is a friendly language for new learners of programming. A system programming language as the forefront of industry, swift language can be easily expressed and understood as a scripting language. It supports "playgrounds" technology, and as an innovative feature, developers can use it to run Swift's immediate operations and visually see results without recompiling the program from scratch.

Swift integrates the wisdom of modern computer languages into the essence of Apple's software development. The Swift compiler is optimized for performance, and the language itself is optimized for the existing development environment, with functionality and performance being balanced and developed. It can design programs that are small enough to "Hello World" programs, which are large to the operating system level. All of this makes the swift language more appealing to both developers and Apple companies.

Using the swift language to write iOS and OS X apps will be very enjoyable, and it will continue to develop new language features and include more features. We are ambitious for Swift's development, and we can't wait to send this tool to everyone to use and create.

The swift start trip

Learning a new computer language is traditionally started by writing a program that prints "Hello world" on the screen. In Swift, we use a word to achieve it:

println ("Hello, World")

If you have written a C or objective-c language, this syntax in Swift looks familiar, but it is a complete program. You do not need to import a separate function library in order to get input/output or string processing. Writing global code is typically used for entry points in a program. You don't need to write a main function, and you don't need to write a comma for each statement as the end.

This introductory trip will give you enough information to start writing Swift code, and you can accomplish a lot of programming tasks. In the process if you do not understand some of the content and do not worry, we will be in the other chapters of the book to explain the details.

Comments:

For better experience development, use playground to edit the code for this section in Xcode. Playgrounds allows you to edit the code while you see the results of the code running immediately.

Simple assignment:

For you to take a constant and a variable as a variable. The value of a constant does not have to be passed to the compiler every time, but you must assign a value to it in advance. This means that a constant can be assigned one time and multiple references.

var myvariable = 42

myvariable = 50

Let myconstant = 42

The same type can be assigned to constants and variables, but you do not have to explicitly define the type each time, and the compiler can automatically judge. When you create a constant or variable, assigning a value lets the compiler know what type it belongs to. In the example above, the compiler specifies that the myvariable variable belongs to an integer type because it is initialized with an integer.

If the initial value cannot express enough type information (or no initial value), a colon and a specified type are appended to the variable.

Let Implicitinteger = 70

Let implicitdouble = 70.0

Let explicitdouble:double = 70

Practice:

To explicitly create a constant of a floating-point type with a value of 4

Assignment does not automatically implicitly become another type, and if you need to change an assignment to another type, you need to explicitly create an instance of the target type.

Let label = ' the width is '

Let width = 94

Let Widthlabel = label + String (width)

Practice:

Try removing the last line of the string conversion declaration to see what's wrong?

Here's a simpler way to include a value in a string: Write the value in parentheses and write the slash in front of the parentheses, as shown in the following example:

Let apples = 3

Let oranges = 5

Let applesummary = "I have \ (apples) apples."

Let fruitsummary = "I have \ (apples + oranges) pieces of fruit."

Test:

The use of \ () in a string contains a floating-point calculation and also contains a name and a message for a person.

Use square brackets ([]) to create arrays and dictionaries, and then use index subscripts in brackets to access the values of each element.

Var

Shoppinglist = ["Catfish", "water", "tulips", "Blue paint"]

SHOPPINGLIST[1] = "Bottle of Water"

var occupations = [

"Malcolm": "Captain",

"Kaylee": "Mechanic",

]

occupations["Jayne"] = "public Relations"

We use initialization syntax to create empty arrays and dictionaries

Let Emptyarray = string[] ()

Let emptydictionary = dictionary<string, float> ()

If you can infer the specified type, you can write an empty array [] and an empty dictionary [:]-For example: When you set a new variable or pass value to the function's parameters.

Shoppinglist = []//shopping and placing all kinds of things.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.