Swift Language Overview

Source: Internet
Author: User

Variables and constants

  Swift declares a variable with Var and let declares a constant.

Myconstant cannotbe modified   

Type deduction

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

" Swift "  "Swift"//Automatic inference type is string

Once a swift variable is declared, the type cannot be modified, and in the example above, STR can only be a string type, after which no assignment can modify its type.

So we say that types can often be omitted, but some places still cannot be omitted. Especially on the collection type, for example a dictionary that would have nsobject, because the initialized string is inferred as a dictionary of exactly the string.

There is a wonderful place where Swift can use any Unicode character as a variable name, that is, Chinese, emoticons, are all possible.

" Swift "  "haha"

Strings string

Note that the string generated here is not nsstring. He's not even an object, and we can see it in the definition:

struct String {    init ()}

Like string and cfstring, he is a struct! However, string can be substituted with NSString, and the API is generic.

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 use [] to declare an empty array, using [:] To declare an empty dictionary. 

arrays, like string, are also structs. However, the array does not have the Nsarray API feature. You need to convert the display to Nsarray to call its methods.

Enumeration

By looping the enumeration, that is,

 

 -     while flag {enum          for var i=1;i<; + +     1.. Ten

Control flow

swift The conditional statement contains ifswitch for-inforwhile and do-while

Let individualscores = [103]0 for inch individualscores {       if        3       else  {        1    }}
Single-channel Branch

In an If statement, the condition must be a Boolean expression
 the    ifBlablabla { +    }  -    Else{  theNote: The curly braces here cannot be omitted. Multi-channel BranchBayi    SwitchNum the    {  the     Case 1:  -       -     Case 2:  the       the    default:
}

Note: You no longer need to write break, and default must have! and must have all the conditions to cover, in general, there will be default, but if used in the enum, you can use case to fix.

For case, it is not only possible to write cases 1 and 2, but also to write Case 1, 2, 3, 4, and perhaps even the case, 1, 2, 3, 4, 7..10

Nullable types

combined iflet?

" Hello "   = = Nilvar optionalname:string"John Appleseed" " hello! " if let name = optionalname {    "Hello, \ (name)"} 

? The expression is: Optional. The wording is as follows:

var possiblevalue:int? = animallegs["Xiaohaha"]

1 if possiblevalue {

2 println ("Notnil")

3}

4 Else {

5 println ("111")

6}

!the function is to break through the optional and get the internal value +    ifPossiblevalue { theLet value = possiblevalue! -println"Xiaohaha has \ (value) legs")  $    }  the    Else {  theprintln"> <")  theBut here, there is a concise wording, more beautiful and elegant.  the    ifLet value =Possiblevalue -    {  inprintln"Xiaohaha has \ (value) legs")  the    }  the    Else  About    {  theprintln"> <")  the}
If the optional value of the variable is nil, the condition is judged to be false and the code in the curly braces is skipped. If it is not nil, the value is assigned to the constant behind let, so the value can be used in the code block.

Other loops

For-in can also be used to traverse a dictionary in addition to traversing an array:

Let interestingnumbers = [    "Prime": [2,3,5,7, One, -],    "Fibonacci": [1,1,2,3,5,8],    "Square": [1,4,9, -, -],]var Largest=0 for(Kind, numbers)inchInterestingnumbers { forNumberinchNumbers {ifNumber >Largest {largest=Number}}}  Largest

While loops and do-while loops:

2  while  - {    2  2 do {    2while  - m

Swift supports traditional for loops, and also can be combined by : (Generate an interval) and for-in implement the same logic.

0  for inch 0.. 3 {    +=0for03; + +    I {1 }secondforloop

Swift Language Overview

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.