Getting Started with Swift

Source: Internet
Author: User
Tags mul

Import foundation//&&&&&&&&&&&&&&&&& In Swift, the end is indicated by a space. &&&&&&&&&&&&&&&&&&/*// After this definition is defined, the value that follows is only a string. Let name:string = "Wang Liang";//hermit type inference, by the initial value to infer the type of the constant let gender = "male"; let Wanglang = "wicked"; let??  = "laugh";//The value of a constant defined with let can never be changed//definition variable var age = 100var Height:int =//swift when defining a variable or constant, you must have a certain type. Let A:int = 10let b : String = "Hello" Let C:character = "A" Let Flag:bool = falselet D:bool = true;//Note: The value is never converted to another type by a hermit, if a conversion is required. Please show the conversion;  /It is not possible to arithmetic two variables or constants of different types directly. Need to write: variable (constant) A + target type blet name1 = "Lanou ' s age is" let Age1 = 3;let words = name1 + String (age1) print (words)//operator overload. The addition operation is automatically stitched together. *///let string = "Good"////creates a string with a value of NULL//let string1 = ""//let string2 = string ()//print (string1)//var Var_ str = "Hello Lanou"//var empty_str1 = ""//var Empty_sty2 = String ()////if empty_str1.isempty//{//print ("EMPTY_STR1 is empt Y ")//}//string link//Create string variable//var var_str =" http://"//var_str + =" www.lanou3g.com "//print (var_str)//////Create string constants. Note Constants cannot be modified//let con_str = "Hello"////gets the length of the string//let length = con_str.characters.count//print (length)//string interpolation//let Mul = 3.0//let content = "\ (mul) *2.5 = \ (mul*2.5)"//print (content)//string comparison//let Name3 = "Xiaohao"//let name2 = "lanou3g"//if name 3 = = Name2//{//print ("\ (Name3) equals\ (name2)")//}//else{//print ("Not Equal")//}//////practice one//Create two variable of type string with initial value "a" "B" and exchange the value of two variables.//var smile = "a"//var Cay = "B"//var ww = ""////WW = Smile//smile = Cay//cay = Ww////print (WW)//print (smil e)//print (Cay)//%%%%%%%%%%%%%%%% arrays%%%%%%%%%%%%%%%%//let array = ["Zhang San", "John Doe", "Harry"];//print ("Zhao Liu \ (Array)")////// If the data stored in the array is not of the same type, then the type of the array is nsobject.//var array1 = ["Wang Deliang", "Big Wave", "Peng Hui", 401]//print ("array1")////Specify the data type of the array in advance.//var Array2: [Int] = [1, 2, 3]//////Swift can change the value of the data by direct access to the array subscript//array1[3] = "Chongdong"//////appends an element to the array.//array1.append (401)////use of transport Fugazai appends an element to the array.//array1 + = [502]////Insert a data//array1.insert ("Peng Fai", atindex:2)////let array5 = ["1", 2]/You want to insert a collection type of data in the array (the collection is the same type as the group)//array1.insertcontentsof (Array5, at:2)//delete all//array1.removeall ()//Remove two//from the beginning Array1.removefirst (2)//Total Delete but reserve memory//array1.removeall (keepcapacity:true);//delete last element//array1.removelast ()// Delete an element of a location//array1.removeatindex (1)//from a few to a few//let Range:range = Range.init (start:1, End:4)//array1.removerange (range) Print (array1)//%%%%%%%%%%%%%%%%%%%% dictionary%%%%%%%%%%%%%%%%%%%%%%//defines a dictionary//let dictionary = ["a": 1, "B": 2]//print ( Dictionary)////if the type of key in the dictionary is different, then the type of key is Nsobject//let dictionary1 = ["A": 1, 2: "B"]//print (dictionary1)////specify key in advance and The type of value creates a dictionary//let dictionary2: [Int:string] = [1: "A", 2: "B"]//print (DICTIONARY2)////can access the elements in the dictionary directly from the key value//print ( DICTIONARY2[1])//////variable////var dictionary3 = ["Small satsuma": "Tabby cat", 114:111]////Modify//dictionary3["small satsuma"] = "big wave"////does not exist, it will plug into//dictionary3["egg"] = "Jianguo"////let a key value of nil means delete this key and corresponding value//dictionary3[114] = Nil//print (dictionary3)// Swaps the value of the underlying array.//var array = [NSObject] ()//array + = ["Hooper"]//array.append ("Wang Deliang")//print (array)////let string = array[0]//array[0] = array[1]//array[1] = string//print (array)//@@@@@@@@@@@@@@@@ Ganso @@@@@@@@@@@@@@@@@@@@@@@@//let type = ("King's Coming", ["Unicom", "Dirty Don"])//print (type)//print (type.0)//subscript////let type1 type1 (String, Int) = ("Zhao si", "//print")////by assignment to value//let type2: (name:string, age:int) = Type1//print (type2.age)//  Practice////var Dictionary = ["number": "Name": "Zhang Fei", "Gender": "Male", "age": 24]//var Stuarray = [Dictionary]////var type = (dictionary)//var type1 = (stuarray)//print (type)//print (type1)//################ #循环结构 branch Structure ################//for ( var i = 0; I < 100;  i++) {//print (i)//}//1...10 closed interval includes 10.  1..<10 half open interval, excluding 10//for i in 1...10{//print (i)//}//var array = ["Dog", "??", "Cat"]//for A in Array//{//print (a)//}//var Dictionary = ["Wangwang": "Dog", "Doudou": "Cat"]//for (b, C) in Dictionary//{//print (b, c)//}//var number = 0//while n Umber < 10{//print (number)//Number++//}//var Number1 = 0//repeat{//number1 + = 1//print (nUmber1)////}while Number1 < 10////if 1 = = 1//{//print (1)//}//optional value, add a question mark after the type of the variable, indicating that the variable is optional and can be set to Nil;//var a: String? = Nil;//var string:string? = "Asda"//if (String! = nil)//{//}////let age = 3////Swift inside each case comes with a break////if you want to run through, you can use the Fallturough keyword. Switch age{//case 1://print ("age = 1")//case 2://print (' age = 2 ')//case 3://print ("age = 3")//Fallthroug    H//case 4://Print ("Age = 4")//default://print ("age = 5")////}////can get values based on interval////switch age{//case 0...10:// Print ("thick with Bo")//case 11...20://print ("Big Butt")//case 21...30://print ("Mengwang")//default://print ("Old pervert")//}//////va r A = 0////switch can add a where condition to the case.//switch a {//case var value where a < && a > 0://value++//pri NT (value)//default://print ("error")//}//let point = (10, 100)////to a meta-ancestor "_" means that the element that ignores this position is omitted.//switch points {//case (10, 1 0)://Print ("First quadrant")//case (_, Ten)://Print ("One, Four quadrants")//case (Ten, _)://Print ("One, Three quadrants")//case (_, _)://PRINT ("Where is the Love")//}//print (point)//%$$$%$%$%$%#$%@!% function [email protected]#$!$%#$!#$%^!#$!//no parameter, no return//no parameter has multiple return values/ /There is no return//have a parameter return//parameter has more than one returned value//inout function//function definition Format//func + function name (parameter list), return value type {}//return value type not write no return value//func text (), Void {//pri NT ("I am a non-parametric and no return worthwhile function")//}//text ()//////non-parametric return//func Text1 ()->string//{//return "I am a function without a reference and return"//}//let String = Text1 () Print (string)////////has no return value//func text2 (name:string, Age:int) {//print ("Love fights Addow is \ (name) age is \ (ages)")//}//text2 ("Huangjun") , age:100)//has a parameter return value////parameter is a string and Int returns an array//func Text3 (name:string, age:int)->array<nsobject>//{//let array:array<nsobject> = [Name, age]//return Array//}//print (Text3 ("Zha", age:404)) the external parameter name of the//////function The first name represents the external parameters Name, when the external use of the function is only the name, the second parameter represents the internal parameter name, when the function inside the use of this parameter is used when the name.//func Text4 (number mynumber:int, name myname:string) {//print ("My name is \ (myName), my number is \ (MyNumber)")//}//text4 (number:100, Name: "Zhao Zilong")////////no parameter has multiple return values of//func Text4 (), String , Int) {//return ("Zhang San", 108)//}//let type =Text4 ()//print ("\ (type.0), \ (TYPE.1)")//parameter has multiple return values//func text5 (name:string, Age:int)--(String, Int)//{//return (  Name, age)//}//let type1 = Text5 ("Zhang", age:10)//print ("\ (type1.0), \ (type1.1)")//////////function nesting//func text6 () {//Func               Text1 () {//print ("First layer")//Func Text2 () {//print ("Second layer")//Func Text3 () {// Print ("third layer")//}//Text3 ()//}//Text2 ()//}//Text1 ()//}//text6 ()//////////// The functions in the InOut function/////////////////////////swift are defined by a let, and cannot be changed if you want to change the definition of//func by using Var text7 (inout name:string) {//name = "Zhao si"//}//var name = ""//text7 (&name)//print (name)//let type = (2, 8)//switch Type{//case (_, 1 ... 5)://print ("Heh hehe")//case (_, 5...7)://print ("hehe hey")//default://print ("roll rolling")//}

Getting Started with Swift

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.