A Swift Tour

Source: Internet
Author: User

Directory

    • Directory
      • About Swift
      • Hello World
      • Variables and constants
      • Data type conversions and string interpolation of strings
      • Arrays Array and Dictionary dictionary

About Swift

SwiftIt is a C Objective-C new programming language that has been developed iOS and OS X applied (published by Apple on June 7, 2014). Swift is freed from the constraints of C language compatibility, employs a secure programming model and adds modern features to make programming easier, more flexible, and more fun. Swiftat the beginning of the birth, in a mature and well-received Cocoa Cocoa Touch framework of support, you can reposition the software development work.

SwiftIt was a programming language that took years to build. Apple has laid the groundwork for swift by improving our current compilers, debuggers, and the underlying structure of the framework. We ARC(Automatic Reference Counting - 自动引用计数) have simplified memory management. Our framework stack is built on a Foundation Cocoa solid foundation of these two frameworks and has been fully modernized and standardized. Objective-Cit has gradually perfected itself, supported, blocks and collection literals , as well modules , enabled the framework to adopt modern language techniques in the absence of destruction. Based on this foundation, we are now able to introduce a new programming language for Apple development in the future.

SwiftWill make Objective-C the developer feel very familiar. It takes the Objective-C readability of parameter naming and the dynamic object model. It can seamlessly access existing Cocoa frameworks as well as with Objective-C mixed programming. SwiftIt also introduces many new features and the combination of process-oriented and object-oriented parts of the language

SwiftVery friendly to the new programmer. It combines the high-performance (performance) and scripting language interactivity (Interactive) of the compiled language. What it supports playgrounds is an innovative feature that allows programmers to Swift see the results of the code immediately, without having to compile and run the app.

SwiftCombine modern language and Apple's engineering culture with great wisdom. The compiler is fully functioning in terms of performance, and language is fully demonstrated in the development field, without any compromises between them. Its design from the "hello world" scale of the project to a complete operating system are fully considered. All that makes it Swift a boon for all developers and Apple's future.

Swiftis a very good way to develop iOS and OS X application, and will continue to be more perfect in the future with new features and new features. We Swift are very confident about the future and we can't wait to see what you are using to develop the product.

Hello World

SwiftCsimilar to language, with a print statement to output“hello world”

println("hello world")

At this point, you'll think of a lot of questions, main where are the functions? How do I get a println("hello world") semicolon after a statement? How is the return value of the program not? This is also Swift a major feature of the language, the program does not need to write any main functions, the execution order by default from top to bottom. There is no need to add a semicolon ";" after each statement.

Variables and constants

Cdefining constants in the language should be const defined as keywords, while in the Swift we use let keywords.

let10
    • We've defined myConstant this constant with a value of 10.

CDefine variables in language we don't have any keywords, and Swift we use keywords var to define

var myVariable = 42
    • We've defined myVariable this variable with a value of 42.

This time wit like mine you have doubts again, the constant variable data go where? This Swift is also the language characteristics. Swiftdepending on the variable (the variable mentioned here includes the constant and the variable below if it is not a distinction is the meaning ) when the initial value of the declaration, automatically capture the data type ("What?!"). Automatic capture of data type, which is too hanging up the sky!! ”)。 Yes Swift , that's the function. The question that comes up is that if I want to declare a variable and not assign a value to it, it captures a ghost. Indeed, if you are declaring a variable, if you write it like this let noInitialValue or the var noInitialValue compiler will make an error. This time you want to specify its data type, and declare it the following way.

varnoInitialValue: Intno10
    • The above declares a variable, but does not assign an initial value and assigns it to the next line
    • Note that you must never let have an initial value for a variable declaration defined by a keyword, because a constant cannot be changed after the declaration.
Data type conversions and strings ( String) interpolation

The data type of the variable is not implicitly convertible. What do you mean? In C language, floating-point variables can be directly assigned to integer variables, but will result in the loss of precision, and will not report syntax errors. While Swift this is not allowed, it must be explicitly converted.

//隐式转换方式,这样是错误的var12.5var intValue: IntintValue = floatValue
    • The above is the implicit conversion method, this is the wrong
//显式转换方式,这样是正确的var12.5var intValue: IntintValue = Int(floatValue)
    • The above is the explicit conversion method, so that is correct

The same type of int can also be converted into String a

//10Applelet10let"Apple"letString(numberOfApples) + fruite
    • numberOfFruiteThe value above is "10Apple"

Data that can be converted to string types is also quicker to convert 字符串插值 by using a "\ ()" In the string, with a variable that can be converted to a string within the parentheses.

//10Applelet10let5let"\(numberOfApples + numberOfOranges)Fruite"
    • numberOfFruiteThe value above is "15Fruite"
Array Array) and dictionaries ( Dictionary)

Both numbers and dictionaries are defined as "[]" in the form of a square bracket.

//数组的定义方式var? ?shoppingList? = [?"catfish"?, ?"water"?, ?"tulips"?, ?"blue paint"?]?shoppingList?[?1?] = ?"bottle of water"
    • shoppingListThe value above changes to? " Catfish "?,?" Bottle of water "?,?" Tulips "?,?" Blue paint "

Initialize empty array (with memory address, but array element is 0)

//指定数据类型的空数组的定义方式?let? ?emptyArray? = [?String?]()
    • emptyArraythe number of elements above is 0, but it has been determined that the elements within the array must be of String type "

How to define an empty array of any type

//任意数据类型的空数组的定义方式?var? anyType?EmptyArray? = [?]
    • anyType?EmptyArraythe number of elements above is 0, but cannot be anyType?EmptyArray[0] = 1 assigned in this way, only the known array is assigned to the array
//字典的定义方式?var? ?occupations? = [?    ?"Malcolm"?: ?"Captain"?,?    ?"Kaylee"?: ?"Mechanic"?,?]?occupations?[?"Jayne"?] = ?"Public Relations"
    • occupationsThe value above changes to? [" Malcolm ":" Captain "?,?" Kaylee "? : "Mechanic",? "Jayne": "Public Relations"?
    • The dictionary is stored in the form of a key-value key-value pair, and it is indexed by the key corresponding value , "Malcolm" is key , this key corresponds value to "Captain"

Initializes an empty dictionary (with memory addresses, but with 0 dictionary elements)

//字典的定义方式?let? ?emptyDictionary? = [String: Float?]()
    • The above emptyDictionary is an empty dictionary, there is no element, its key String type, corresponding value to the Float type

key value Empty dictionary definitions of any type, type

//任意key类型,value类型的空字典定义方式?var? anyType?EmptyDictionary = [:?]
    • The above anyType?EmptyDictionary is an empty dictionary, there is no element, it is key any type, corresponding value to any type, the same can not be passed anyType?EmptyDictionary["myKey"] = "myValue" the form of assignment, can only tell the known dictionary variable assignment to it

A Swift Tour

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.