[IOS translation] The Swift Programming Language series: Welcome to Swift-01

Source: Internet
Author: User

Book directory:

1. Welcome to Swift

Ii. Language Guide

Iii. Language Reference

 

/*

Translator's nonsense:

I stayed up late a few hours ago and watched WWDC. I was so excited that this year was very promising! When Swift came out, it was a dumb to learn a new language. This article is from Apple's official book "The Swift Programming Language", which is about 500 pages and has been downloaded on The official website of apple. Swift has never been used in practice. This translation must have various mistakes. I will keep updating and correcting them.

-- (Blog and Sina Weibo)

*/

 

1. Welcome to Swift

1. About Swift

Swift is a brand new programming language for iOS and OS X applications. It is built in the best C and Objective-C languages without the compatibility constraints of C languages. Swift adopts a Secure Programming Model and adds modern functions to make programming easier, more flexible, and more interesting. Backed by the mature and favored Cocoa and Cocoa Touch frameworks, Swift is an opportunity to rethink software development.

After several years of development, Apple laid the foundation for Swift by improving the existing compiler, debugger, and framework structure, and simplified memory management through the Automatic Reference Counting (ARC. The Swift framework is built on solid foundations and Cocoa and has been modernized and thoroughly standardized. Objective-C has gradually developed to support blocks, collection literals and modules, so that the framework uses modern language technology without interruption. Thanks to this foundation, we can introduce a new language for the future of Apple's software development.

Swift looks familiar to Objective-C developers. It has the readability of Objective-C naming parameters and the power of dynamic object models, provides seamless access to the existing Cocoa framework and Objective-C hybrid editing capabilities. Based on this commonality, Swift introduces many new features, unified programs, and object-oriented languages.

Swift is friendly to new programmers. As a expressive and entertaining scripting language, Swift is the first system programming language to achieve industrial quality. It supports playgrounds, a new function that allows programmers to debug Swift code and view results immediately without repeatedly compiling and running programs.

Swift combines the best modern language thinking and wisdom-from a broad culture of Apple engineers. The compiler optimizes performance and language development without any sacrifice. Its design extends from "Hello World" to the entire operating system, making Swift a meaningful investment for developers and Apple.

Swift writes iOS and OS X applications in an excellent way and will continue to develop new features and functions. Swift is full of ambition and we can't wait to see what you can use it to create.

 

2. Swift Overview

Traditionally, we recommend that the first program in the new language print "Hello, World" on the screen. In Swift, only one line is required.

  

If you have written code in C or Objective-C, this syntax looks familiar-in Swift, this line of code is a complete program, you do not need to import a separate library for input/output or string processing functions. Code written globally serves as the function entry point. Therefore, you do not need a main function or a semicolon at the end of each statement.

This overview shows how to complete various program tasks to give you enough information to start writing Swift code. If you don't understand anything, don't worry, all the details in this overview will be explained in detail later in this book.

NOTE: For the best experience, open this chapter as a playground in Xcode. playground allows you to edit the code list and view the result immediately.

 

2. 1. Simple Value

UseLet creates a constant and var creates a variable.. The value of a constant does not need to be known during compilation, but you must assign it a value once, this means that you can use constants to name a value that you decide to use in many places.

  

Constants or variables must have the same type and the value you want to assign to them. However, you do not always have to specify the type. Provides a value. When you create a constant or variable, the compiler can deduce its type. In the preceding example, the compiler deduced that myVariable is an integer because its initial value is an integer.

 

If the initial value does not provide sufficient information (or if there is no initial value), you can specify the type after the change and separate it with a colon.

  

Test: Create a constant of the float type and the value is 4.

 

The value will never be converted to another type. If you need to convert a value to a different type, create an instance of the desired type explicitly.

  

Test: What error have you got when you try to remove the String Conversion from the last line?

 

There is also a simpler method to include string values: Write the values in brackets and write a backslash (\) before the parentheses (\). For example:

  

Test: Use (\) to include a floating point Computation in a string and a certain name in a greeting.

 

Use square brackets ([]) to create arrays and dictionaries, and write the index and key in brackets to access their elements.

 

To create an empty array or dictionary, use the initialization syntax.

 

If the type information can be inferred, you can write an empty array as [] and an empty dictionary as [;] -- for example, when you set a new value or parameter of a variable, it is passed to the function.

  

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.