Declares a constant let Maxnumberofstudents:int = 47//declares a variable, assuming that it is not initialized at the time of the Declaration, that it needs to display the label whose type var currentnumberofstudents = 23//must be initialized before use. Currentnumberofstudents + = 1//floating point data is self-inferred as a double type, assuming that a float type is required, the specified float type to be displayed is let Score:float = 90.5;let Englishscore = 80let Chinesescore = 90.5;//Different types of data can not be directly operated, you need to manually convert to the same format, the format is: Type (Value) Let Allscore = Double ( Englishscore) + chinesescoreprintln ("Int8 max value = \ (Int8.max)") var name:string = "HMT"//Display declaration a character type let sex:charact er = "M" var names:array<string> = ["HMT", "WDQ1"]var namest:string[] = ["HMT", "WDQ"]//var nameq:nsarray = ["AAA", " BBB "," CCC "]namest + =" QQQQQ "//array even if immutable, can still change its value namest[1] =" 1111 "//2 points half open half closed 3 points full closed namest[1..2] = [" Buzhidao "," Heheheh Eh "];p rintln (" \ (namest) ") for (Index,value) in enumerate (namest) {println (" \ (index) \ (value) ")}var sexdic:dictionary& Lt string,string> = ["Zhuwenpeng": "M", "Zhaoweisong": "M"]//assuming that the key value pair does not exist in the dictionary, it will be added; if it exists, it will replace the change sexdic["Chenfengchang"] = "M" println (sexdic["Chenfengchang"]) for (Key,value)In sexdic{println ("key = \ (key) value = \ (value)") println (sexdic["Chenfengchang"])}//"_" means ignoring a location in the tuple for (_,value ) in sexdic{println ("value = \ (value)")}let Firstsex = sexdic["Chenfengchang"]//string inference with "= =" let Ismale = Firstsex = " M "//Inference only accepts Boolean if ismale{}else{}//tuple let status: (Int, String) = (404," not Found ") println (" StatusCode = \ (status.0) status Message = \ (status.1) ") Let Status1: (statuscode:int,statusmessage:string) = (statuscode:404,statusmessage:" Not Found " ) println ("StatusCode = \ (status1.statuscode) StatusMessage = \ (status1.statusmessage)") Let (statuscode,statusmessage = (404, "not Found") println ("StatusCode = \ (statusCode) StatusMessage = \ (StatusMessage)") Let Countstr = "333"//ToInt () party The method returns an optional variable of type int, possibly empty, plus "?" Retouching let Count:int? = Countstr.toint () if count{let Realcount = count! Force parsing println ("Realcount = \ (realcount)")}else{}//first infers if Count has a value, assuming that there is a value to pay realcount, assuming no entry into ElseIf let Realcount = count{println ("Realcount = \ (realcount)")}else{}letPoint = (x:2,y:2) switch (point) {case (0,0): println (") Case (Let x,0): println (" ") case (0,let y): println (" ") CAs E (Let X,let y): println ("")}switch ("") {case (let X,let y) where x = = Y:println ("Point on x=y this line") Case (Let X,le T y) where x = =-y:println ("") case (Let X,let y): println ("")}let number = 122switch (number) {Case 0...9:PRINTLN () Case 10...99:println () Case 100...999:println () Fallthrough//equivalent to switch in C language without break Effect default:println ()} Let tempstring = "Iyoipolk" tempname:for F-tempstring{switch (letter) {case "a", "E", "I", "O", "U": println ("Letter is a vowel") Break tempname default:println ("Letter is a consonant")}}
Swift First Experience (i)