Introduction to the Swift programming language

Source: Internet
Author: User
Tags arrays touch


What is Swift?



Swift is Apple's programming language in WWDC 2014, quoted by the Swift programming language:



Swift is a new programming language for iOS and OS X apps that builds on the best of C and objective-c without the Constra INTs of C compatibility.



Swift adopts safe programming patterns and adds modern features to make programming easier more flexible and more fun.



Swift ' s clean slate backed by the mature and much-loved cocoa and cocoa touch frameworks-is-opportunity to imagine Software development works.



Swift is the industrial-quality Systems programming language This is as expressive and enjoyable as a scripting Lang Uage.



To put it simply:



Swift is used to write iOS and OS X programs. (It is not expected to support other cock-wire systems)



Swift absorbs the benefits of C and objective-c and is more powerful and easy to use.



Swift can use the existing cocoa and cocoa touch framework.



Swift has both the high performance (performance) and scripting language interactivity (interactive) of the compiled language.



Swift Language Overview



Basic concepts



Note: This section of code originates from Swift tour in the swift programming language.



Hello World



Similar to the scripting language, the following code is a complete Swift program.



1



println ("Hello World")



Variables and constants



Swift uses VAR to declare a variable, let declare a constant.






var myvariable = 42



myvariable = 50



Let myconstant = 42



Type inference



Swift supports type inference, so the above code does not need to specify a type, if you need to specify the type:






Let explicitdouble:double = 70



Swift does not support implicit type conversions (implicitly casting), so the following code requires an explicit type conversion (explicitly casting):






Let label = ' the width is '



Let width = 94



Let width = label + string (width)



String formatting



Swift uses the form of (item) for string formatting:






Let apples = 3



Let oranges = 5



Let applesummary = "I have (apples) apples."



Let applesummary = "I have (apples + oranges) pieces of fruit."



Arrays and dictionaries



Swift uses the [] operator to declare arrays (array) and dictionaries (dictionary):





var shoppinglist = ["Catfish" "Water" "Tulips" "Blue paint"]



SHOPPINGLIST[1] = "Bottle of Water"



var occupations = [



"Malcolm": "Captain"



"Kaylee": "Mechanic"



]



Occupations["Jayne"] = "Public Relations"



You typically use the initializer (initializer) syntax to create an empty array and an empty dictionary:





Let Emptyarray = string[] ()



Let emptydictionary = dictionary< string float> ()



If the type information is known, you can declare an empty array using [], and declare an empty dictionary using [:].



Control flow



Overview



Swift's conditional statement contains if and switch, the loop statement contains For-in, for, while, and Do-while, and the loop/Judge condition does not require parentheses, but the circular/judgmental (body) required bracket:





Let individualscores = [75 43 103 87 12]



var Teamscore = 0



For score in Individualscores {



If score > 50 {



Teamscore + 3



} else {



Teamscore + 1



}



}



Nullable types



Combined with if and let, the nullable variable can be easily handled (nullable variable). For null values, it needs to be added after the type declaration. explicitly indicate that the type is nullable.






var optionalstring:string? = "Hello"



Optionalstring = = Nil



var optionalname:string? = "John Appleseed"



var gretting = "Hello!"



If let name = optionalname {



gretting = "Hello (name)"



}



Flexible switch



The switch in Swift supports a variety of comparison operations:




Let vegetable = "red pepper"



Switch Vegetable {



Case "Celery":



Let vegetablecomment = "add some raisins and make ants on a log."



Case "cucumber" "watercress":



Let vegetablecomment = "This would make a good tea sandwich."



Case Let X where X.hassuffix ("Pepper"):



Let vegetablecomment = "is it a spicy (x)?"



Default



Let vegetablecomment = "everything tastes good in soup."



}


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.