Original text (http://www.cnblogs.com/lkvt/p/3765349.html)
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. And there is no compatibility limit for C Language. Swift adopts a secure programming model. Added modern features to make programming easier, more flexible, and more Fun.
Swift's support for the mature and pampered Cocoa and Cocoa Touch Framework is another opportunity to envision 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. has been modernized and thoroughly standardized. Objective-c itself has evolved to support blocks, collection literals and modules, enabling the framework to use modern language technology without Interruption. Thanks to this foundation, we are able to 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. Provides seamless access to existing cocoa frameworks and the ability to integrate with OBJECTIVE-C. Built on this common Denominator. Swift introduces a number of new features, a unified program, and an object-oriented language component.
Swift is friendly to the new program Ape. As an expressive and entertaining scripting language, It is the first system programming language to obtain industrial Quality.
It supports playgrounds, a consenting ape to debug Swift code and see results Immediately. Without having to repeatedly compile the new functionality of the execution Program.
Swift combines the best of modern language thinking and wisdom-from the vast Apple project master Culture. The compiler is optimized for performance. Language has also been developed to optimize without sacrificing whatever. 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 Functions.
Swift is ambitious and we can't wait to see what you can create with it.
2.Swift Overview
The first program that traditionally suggests a new language should print "Hello, world" on the screen, but in swift, this only needs to be done in one LINE.
println ("hello,world")
Suppose you have written 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 features like input/output or string processing. Code written on a global scale is used as the entry point for Functions. So you don't need a main function, and you don't have to write a semicolon at the end of each statement.
This overview gives you enough information to start writing Swift code by showing you how to complete the various program tasks. If You're not clear, don't worry, all the details in this overview will be explained later in this Book.
Note: to get the best experience. Open this chapter as a playground in xcode, playground agree 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 at a Time. This means that you can use constants to name a value that you decide to use in many Places.
var myvariable = 42
myvariable = 50
Let myconstant = 42
Constants or variables must have the same type. The value you want to assign to it. however, you do not always have to understand the type of writing. Provides a value when you create a constant or variable that allows the compiler to determine its type. In the demo example above, the compiler determines that myvariable is an integer because its initial value is an integer.
Suppose the initial value does not provide enough information (or assume there is no initial value). The type can be set after the change, separated by a Colon.
Let Implicitinterger = 70
Let Implicitdouble =70.0
Let explicitdouble:double = 70
Experiment: Create a constant that understands the float type. and the value is 4.
A value is never absolutely converted to another type.
Suppose you need a value to be converted to a different type, to clearly create an instance of the desired type.
Let label = "the Width is"
Let width = 94
Let widthlable = lable + String (width)
Experiment: try to remove the string conversion from the last Line. What mistakes Have you got?
Another simpler method is to include a string value: write the value in parentheses and write a backslash (\) before the Parentheses. Like what:
Let apples = 3
Let oranges = 5
Let applesummary = "I hava \ (apples) apples."
Let fruitsummary = "I had \ (appel + oranges) pieces of fruit."
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.
var shoppinglist = ["catfish", "water", "tulips", "blue paint"]
shoppinglist[1] = "bottle of water"
var occupations = [
"Malcolm": "captain",
"Kaylee": "mechanic",
]
occupations["Jayne"] = "public relations"
To create an empty array or dictionary. Use the initialization syntax.
Let Emptarry = string[] ()
Let emptdictionary = dictionary<string, float> ()
Assuming the type information can be judged, you can write an empty array for [] and an empty dictionary for [;]--for example, when you set a variable to a new value or a number of arguments to pass to the Function.
Shoppinglist = []
itunes Links:
mt=11 "target=" _blank "class=" gj_safe_a "style=" word-wrap:break-word; Color:rgb (73,123,137); font-family:tahoma, ' Microsoft Yahei ', simsun; font-size:14px; line-height:21px ">https://itunes.apple.com/us/book ... e/id881256329?
mt=11
epub Version number download:Http://pan.baidu.com/s/1ntJnbc9 . Draft 涗-stardust 泦, Juan €-bang ╀笁 arrival Ricoh Ban
calibre download (can see epub on pc):Http://calibre-ebook.com/download
The Swift programming Language Chinese Translation version