Swift (1) Basic syntax

Source: Internet
Author: User

"Fit for object" students with a certain programming foundation

"Scene" while watching while practicing, more practice more learning, natural growth.

"Statement" This article belongs to original work, unauthorized, such as illegally reproduced, to be held liable. Original link: http://www.cnblogs.com/codeAnimal/p/4041545.html

Practice NOTES:

1.var variables
var str = "Hello, playground" println (str)

2.if statements

var a = 20if a > {    println ("If statement, judging condition without brackets");}

3. Ternary operators

var sy = a > 80? "True": "false" println (SY)

4. Specifying the data type

var a1:int = 10println (A1)

5. Annotation symbols

//

6. Data type

var inta:int32 = 12; println (INTA) var floata:float32 = 10.0; println (Floata)

7. Control Flow For-in

Let indi = [10,201,30]var teamscore  = 0for score in Indi {    if score > 50{        println (Score)    }} results printed: 201

Exercise 1: Special characters://. Representation Range


var first = 0
For I in 0..3{
First + = I
}
println (first)

Exercise 2: Calculate the numeric maximum result in the dictionary:  456let nums = ["Prime": [45,456,1,3], "squ": [12,10,32]]var largest = 0for (k,n) in nums{    For num in n{        if num > largest{            largest = num        }    }}println (largest)

8.switch Statement (Note: default, must have)

Let Vege = "Red Peper" var title = "Default msg" switch vege{case    "Red":        title = "Red" Case    "Red", "Peper":        title = "2"; case let y where Y.hassuffix ("Peper")://Special Usage        println (y)        title = "3" default:    title = "SSSs"   } println (title)

9.while

var n = 2;while N < {    n = n*3}println (n)

10. Calculate the length of the string countelements

var s = "Lljkjij" var L = countelements (s) println (L)

11. String equality = =

var str1 = "I am Liuqinghua" var str2 = "I am Liuqinghua" if str1 = = str2 {    println ("Result >" + "= =")}else{    Prin TLN ("Result >" + "! =")}

12. Data type Conversion

Let NUM1 = 3let num2 = 0.1212var pi = num1 + Int (num2) println (PI)

Printed results: 3

. Int? Optional type

var strint = "123" Let Convertednum:int? = Strint.toint ()  //Unsure whether Strint is a pure number or a string println ("\ (convertednum) result \ (convertednum!)") Set a nil value for a variable of an optional type, indicating that there is no value var serverresponse:int? = 404serverResponse = Nil

14. Tuples

Form 1.let httperror = (404, "Fail") println (Httperror.1)//form 2.let HttpError2 = (status:404,descrip: "Fail") println ( Httperror2.status)

15. Arrays

var shoplist:string[] = ["Egg", "Milk"]//append (Append) shoplist.append ("Flour") println (Shoplist.count)//array element number//insert ( Insert) Shoplist.insert ("Maple", AtIndex:shopList.count) println (shoplist)//delete (Removeatindex) Shoplist.removeatindex (0) println (shoplist)//Array length shoplist.count//traverse 1for item in shoplist{    println (item)}// Traverse 2for (Index,value) in enumerate (shoplist) {    println ("Item\ (index+1): \ (Value)")}

Print Result: Index, subscript (starting from 0)

16. Dictionaries

var air:dictionary<string,string> = ["A": "A", "B": "B"]//dictionary append air["c"] = "C" println (air)//Dictionary Delete element Removevalueforkeylet Isdel = Air.removevalueforkey ("a") println (Isdel)//"A" (deleted value) println (air)//Dictionary length The Count Property air.count//dictionary traversal 1for (k,v) in air{    println (k + ":" + V)}//Dictionary traversal 2: Traverse keys and Valuesfor key in air.keys{    respectively println (key)}for value in air.values{    println (value)}//convert the key to an array let Airkey = Arrays (Air.keys) println (Airkey)

17. Functions

Func sumof (Nums:int ...)-int{    var sum = 0    var j = 0 for    n in nums{        sum + = n        j + +    }    return Sum/j    }//Call method var code = sumof (20,20,20) println (code)

18. Class

Class test{    //DECLARE variable    var num:int = 0    var name:string       //Constructor    init (name:string) {        Self.name = name;    }        Custom method, returns String    func simple (), string{            return name;    } var test = Test (name: "Liuqinghua") var name = Test.simple () println (name) print Result: Liuqinghua

  

Swift (1) Basic syntax

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.