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 is built on a solid foundation and cocoa, and has been modernized and thoroughly standardized. 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 the future of Apple software development.
For objective-c developers, Swift looks familiar, with the readability of objective-c named parameters and the power of the dynamic object model, 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 evolve with new features and functionality. Swift is ambitious and we can't wait to see what you can create with it.
2.Swift Overview
The traditional suggestion that the first program of the new language should print "Hello, World" on the screen, in Swift this only needs a single line to complete.
If you write code in C or Objective-c language, this 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. Code written on a global scale acts as an entry point for functions, so you don't need a main function, and you don't have to write semicolons at the end of each statement.
This overview gives you enough information to start writing Swift code by showing you how to complete various program tasks . If you don't understand, don't worry, all the details in this overview 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 many places.
A constant or variable must have the same type as the value that you want to assign to it. However, you do not always have to explicitly write the type. Provides a value that allows the compiler to infer its type when you create a constant or variable. 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 make the type after the change, separated by a colon.
Experiment: Create a constant, explicitly a float type, and a value of 4
The value will never be absolutely 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 parameter passed to the function.