Note: Cocoachina translation team has embarked on the translation of this book and related Materials, the landlord also joined, the full translation after the collaboration of many people will soon let you see.
Translation group: 291864979, want to join the students please enter this group OH. (This series is no longer updated, but the progress of collaborative translation I will be updated in the message, thank you for your support!) )
As of 21:52 on June 4, the first chapter of the book has been translated and the other chapters have been translated by members of the group.
You can go to Cocoachina to follow the relevant information, or go directly to the Git hub to see the latest progress: Https://github.com/CocoaChina-editors/Welcome-to-Swift
Directory:
First, Welcome to Swift
Second, Language Guide
Third, Language Reference
/*
Translator's nonsense:
A few hours ago stayed up watching WWDC, all kinds of excitement, this year is very material Ah! When we see swift coming out, I suddenly get dumbfounded and learn a new language. This article is from Apple's official "The Swift Programming Language" book, about 500 pages, downloaded on the Apple website. Swift everyone has not actually used, this translation must have a variety of errors and omissions, you can forgive me, I will constantly update the amendments.
--(blog park, Sina Weibo) Galbrain the Great
*/
First, Welcome to Swift
1. About Swift
Swift is a new programming language for iOS and OS X apps, built in the best C and Objective-c languages, without the C language compatibility restrictions. Swift uses a secure programming model that adds modern functionality to make programming easier, more flexible, and more fun. Swift's support for the mature and pampered Cocoa and Cocoa Touch Framework is an opportunity to reimagine software development.
Swift has been brewing for years, and Apple has laid the foundation for swift by improving existing compilers, debuggers, and framework structures, and has simplified memory management through automatic Reference counting (ARC). Swift's framework stack is built on a solid foundation and cocoa, modernizing and thoroughly standardizing. Objective-c itself has evolved to support blocks, collection literals and modules, enabling the framework to adopt modern language technology without interruption.
Thanks to this foundation, we can introduce a new language for future Apple software development.
Swift looks familiar to objective-c developers, with the power of objective-c parameter-naming legibility and dynamic object models, providing seamless access to existing cocoa frameworks and the ability to integrate with OBJECTIVE-C. Based on this common denominator, Swift introduces many new features, a unified program, and an object-oriented language component.
Swift is friendly to the new programmer, and as an expressive and entertaining scripting language, it is the first system programming language to get industrial quality. It supports playgrounds, a new feature that allows programmers to debug swift code and see results immediately without having to repeatedly compile and run the program.
Swift combines the best of modern language thinking and wisdom-from the vast Apple engineer culture. The compiler is optimized for performance and the language is optimized for development without sacrificing any more. Its design extends from "Hello World" to the entire operating system, making swift a meaningful investment for developers and Apple.
Swift is an excellent way to write iOS and OS X apps, and will continue to develop new features and functionality. Swift is ambitious and we can't wait to see what you can create with it.
2.Swift Overview
As a rule, the first program in the new language should output "Hello, World" on the screen. In Swift, this only needs to be done in one line.
If you write a C or objective-c code, the syntax looks familiar-in swift, this line of code is a complete program, and you don't need to import a separate library for functions such as input/output or string processing. All of the code in the range is used as the entry point for the program, so you don't need the main function, and you don't have to write a semicolon at the end of each statement.
This overview provides you with enough information to learn to write Swift code by completing various programming tasks. If you don't understand, don't worry, all the details will be explained in detail later in this book.
Note: To get the best experience, open this chapter as a playground in Xcode, playground allows you to edit the code list and see the results immediately.
2.1. Simple values
Using let to create a constant, VAR creates a variable . A constant value does not need to be known at compile time, but you must assign it a value once, which means you can use a constant to name a value that you decide to use in multiple places.
A constant or variable must have the same type as the value you assign to it, however, you do not always have to explicitly write out the type. When you create a constant or variable and assign a value, you can have the compiler infer its type. In the above example, the compiler infers that myvariable is an integer because its initial value is an integer.
If the initial value does not provide enough information (or if there is no initial value), you can type after the variable, separated by a colon.
Experiment: Create a constant, explicitly a float type, with a value of 4
Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly create an instance of the desired type.
Experiment: Try to remove the string conversion from the last line, what error did you get?
There is also a simpler way to include a string value: Write the value in parentheses and write a backslash (\) before the parentheses. For example:
Experiment: Use (\) to include a floating-point calculation in a string and include the name of a person in a greeting.
Use square brackets ([]) to create arrays and dictionaries, and write index and key in parentheses 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 for [] and an empty dictionary for [:]--For example, when you set a variable to a new value or argument to the function.