Introduction to Swift (i)

Source: Internet
Author: User

    1. Constants, variables
      Declaring constants: Let
      Declaring variables: var
      eg. Let connum = 1; var num = 2;

    2. Basic data types
      Int, Float, Double, Bool, tuples, optional
      which
      Tuples Ganso, can contain a collection of different types of values;
      Optional an optional value that indicates that a value can be null (nil), and if a variable is not declared as optional, then it must be given an exact value;

    3. Type conversions
      You need to specify a conversion type: Sometype (val)

    4. Operator
      have similar rules to other languages
      Special rules

      • Can be used for the remainder of float, 8 2.5//equals 0.5;
      • Empty value operator a?? b, the optional type a null judgment, if a contains a value to be unpacked, otherwise returns a default value B, the expression a must be a optional type, the type of the default value B must be consistent with the type of a stored value
      • Interval operator闭区间运算符(a...b); 半开区间(a..<b
    5. String
      Swift's string type is a value type. If you create a new string, the value is copied when it is passed in a constant, variable assignment operation, or in a function/method. In any case, a new copy of the existing string value is created and the new copy is passed or assigned.

    6. Array

var shoppingList: [String] = ["Eggs""Milk"]shoppingList.append("Flour")shoppingList += ["Chocolate Spread","Cheese","Butter"]shoppingList[0"Six eggs"shoppingList.insert("Maple Syrup"0)let mapleSyrup = shoppingList.removeAtIndex(0)forin shoppingList {    println(item)}
    1. Dictionary
varAirports: [String:String] = ["Tyo":"Tokyo","DUB":"Dublin"] for(Airportcode, Airportname)inchAirports {println ("\ (airportcode): \ (airportname)")} forAirportcodeinchAirports.keys {println ("Airport code: \ (airportcode)")}//Airport Code:tyo//Airport CODE:LHR forAirportnameinchairports.values {println ("Airport name: \ (airportname)")}//Airport Name:tokyo//Airport Name:london Heathrow LetAirportcodes =Array(Airports.keys)//Airportcodes is ["Tyo", "LHR"] LetAirportnames =Array(airports.values)//Airportnames is ["Tokyo", "London Heathrow"]

Both arrays and dictionaries store mutable values in a single collection. If we create an array or a dictionary and assign it to a variable, the set will be mutable. This means that we can change the size of this collection by adding more or removing existing data items after creation. Conversely, if we assign an array or dictionary to a constant, it is immutable and its size cannot be changed.

    1. For loop
 Letnames = ["Anna.","Alex","Brian","Jack"] forNameinchNames {println ("Hello, \ (name)!")} LetNumberoflegs = ["Spider":8,"Ant":6,"Cat":4] for(Animalname, Legcount)inchNumberoflegs {println ("\ (animalname) s has \ (legcount) legs")} for varindex =0; Index <3; ++index {println ("index is \ (index)")}
    1. While, do While
whilecondition {    statements}do {    whilecondition
    1. If
var temperatureInFahrenheit = 90if temperatureInFahrenheit <= 32 {    println("It‘s very cold. Consider wearing a scarf."elseif temperatureInFahrenheit >= 86 {    println("It‘s really warm. Don‘t forget to wear sunscreen."else {    println("It‘s not that cold. Wear a t-shirt.")}
    1. Switch
Let Somecharacter:character ="E"SwitchSomecharacter { Case "a","E","I","O","U":println("\ (somecharacter) is a vowel") Case "B","C","D","F","G","H","J","K","L","M","n","P","Q","R","S","T","V","W","x","Y","Z":println("\ (somecharacter) is a consonant")default:println("\ (Somecharacter) is not a vowel or a consonant")}

Here the default statement is required, otherwise the compilation does not pass, and no break is required because only one case is matched.

You can also add conditions to the case to determine where

let  yetanotherpoint = (1 ,-1 ) switch  Yetanotherpoint {case  let  (x, y) where  x = = Y:println (" (\ (x), \ (y)) is on the line x = = y ") case  let  (x, y) where  x = =-y:println ( "(\ (x), \ (y)) is on the line x = = y" ) case  let  (x, y): println (" (\ (x), \ (y)) is just Some arbitrary point ")}//Output" (1,-1) is on the line x = y "  
    1. continue, break, Fallthrough
      continue, and break are the same as other language usages;
      Fallthrough is used in the switch statement to implement the condition through, Equivalent to the absence of a break in Java or other language case blocks:
let integertodescribe = 5  var  Description =  "the number \ (Integertodescribe) is"  switch  integertodescribe {case   2 ,  3 ,  5 ,  7 ,  one , , Span class= "Hljs-number" > , : Description + =  " A prime number, and also " fallthrough  default : Description + =  "an integer." } println  (description) //output "the number 5 is a prime number, and also an integer."  

Getting started with Swift (i)

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.