Swift Learning Swift Programming Tour (i)

Source: Internet
Author: User

The most classic example of learning a new language is the output "Hello world! ”

Print ("Hello world!  ") This is how swift is exported.

If you have used other languages, then it seems to be very familiar. But it's a lot easier than some c.

1, do not need to import some separate libraries, such as input/output or string processing function of the class library,

2. Code that does not need to be written at the global scope is used as an entry point program, so you do not need the main () function,

3. You do not need to write the semicolon at the end of each statement. Is it easy to write like this?

  Simple values

Using let to declare a constant, VAR declares a variable. A constant value does not need to be known at compile time, but you can only assign a value once, we need to assign a value at the time of Declaration, if a compilation error occurs multiple times, such as

A value of a variable can be assigned at the time of declaration, or it can be assigned later.

The types of constants or variables must be the same as the values assigned to them. However, when declaring a type is optional and the declaration is assigned a value, the compiler automatically infers the type. In the example above, the compiler infers that var1 is an integer because its initial value is an integer.

If the initial value does not provide enough information (or no initial value), then you need to declare the type after the variable, separated by a colon. Numeric values are not implicitly converted to other types, and must be explicitly converted, such as converting to a character type if a compilation error occurs without explicit conversion, such as the simplest conversion is to write the value into parentheses, and write a \ Arrays and dictionariesUse [] to create arrays and dictionaries to access elements through an array index or a dictionary key (key). Separate elements with commas.
varShoppinglist = ["Catfish","Water","Tulips","Blue paint"]shoppinglist[1] ="bottle of water" varOccupations = [    "Malcolm":"Captain",    "Kaylee":"Mechanic",]occupations["Jayne"] ="Public Relations "

Let's take a look at the results

Ok! the same as we expected.

Below we create an empty array and dictionary

Let Emptyarray == [String:float] ()//Create a key for a character type, a dictionary with a value of floating-point type

If the type can be inferred, you can use [] and [:] To create an empty array and dictionary.

Control flow

Use if and switch to perform conditional actions, use for-in, for,while,repeat-while to perform loop operations. The parentheses of the condition or loop variable are optional, but the curly braces ({}) of the body of the statement are required.

 let individualscores = [75 , 43 , 103 , 87 , 12   var  teamscore = 0  for  score in   Individualscores { if  score > 50   {Teamscore  + = 3    else   {Teamscore  + = 1  }}print (teamscore)//Output one   

The condition must be a Boolean (Boolean) expression in the IF statement. Here we can see the brevity of swift syntax.

  var  optionalstring:string? = Span style= "color: #800000;" > " hello  "   == nil)  var  Optionalname:string? =  john Appleseed   " var  greeting =  " hello!   " if  let name = Optionalname {greeting  = 
      
        "
       hello, \ (name)  "  }

Output Result:

False

Hello, John Appleseed.

because optionalstring and Optionalname All there is a specific value, but if optionalname=nil words, then Greeting output is hello!

var " John Appleseed " var " hello! " if let name = optionalname {    "Hello, \ (name)"} Else  "Hello, anonymous "}

Output results

False

Hello, Anonymous

Switch supports any type of data and various comparison operations, which are not limited to integers and equal tests

Let vegetable ="Red Pepper"SwitchVegetable { Case "Celery": Print ("Add Some raisins and make ants on a log.") Case "Cucumber","Watercress": Print ("That's would make a good tea sandwich.") CaseLet XwhereX.hassuffix ("Pepper"): Print ("is it a spicy \ (x)?")default: Print ("everything tastes good in soup.")}

The output result is

Is it a spicy red pepper?

Swift Learning Swift Programming Tour (i)

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.