Some swift small knowledge points of collation

Source: Internet
Author: User

Some basic things to know about Swift in the first time, simply sort it out.

ImportUIKitvarnames = []class viewcontroller:uiviewcontroller,uialertviewdelegate {overridefuncViewdidload () {super.viewdidload ()varstringvalue:string!//StringValue = "CHW"//Let Str11 = Stringvalue.hashvalue//var stringvalue:optional<string>        ifLet str = stringvalue {let HashValue = str.hashvalue} Let str1 ="Swift"         forK in str1 {println(k)}varDIC =[0: 0,1: 0,2: 0]varNewdic = dic//check DiC and NewdicDic[0] =1        println(DIC)//[0:1, 1:0, 2:0]        println(Newdic)//[0:0, 1:0, 2:0]        varArr11:array =[0,"LLL", 0]varNEWARR = Arr11 Arr11[0] =1        //check arr and NewArr        println(ARR11)//[1, 0, 0]        println(NEWARR)//[0, 0, 0]//StringLet heart ="??"        println(Heart)//??        //empty stringLet empty =""Let Anotherempty = String ()ifEmpty.isempty {println("Empty")        }Else{println("Not Empty")        }//prefix suffixLet Str11 ="Chenghong Wei"        ifStr11.hasprefix ("ch") {println("prefix is ch")        }ifStr11.hassuffix ("Wei") {println("suffix is Wei")        }//CaseLet up = str11.uppercasestringprintln(UP)//chenghong WEILet low = str11.lowercasestringprintln(Low)//chenghong Wei//selectable values to explore        varInput:string?varINPUT1 =" "        varAge = Input1.toint ()ifAge! =Nil{println("Your is"+ String (age!)) }Else{println("error for input")        }//Traversal arrayLet arr = ["1","2","3"]//        ... equivalent to [0,2] an interval, meaning 0=< i && i<= 2         forI in1... arr.count {println("arr count is \ (arr.count)")println("Arr object index \ (arr[i-1])")        }//        .. < is a half-closed half-open interval, meaning [0,arr.count] (previously, it has changed)         forI in0.. <arr.count {println(i)} for varI=0; I<3; i++ {println("\ (arr)")        }//Array Canadian dollar        varARR1 = ["a"] Arr1 + = ["B"] Arr1 + = ["C","D","E"]println(ARR1)println("arr1 is \ (arr1)") arr1[0] ="F"        println(ARR1)varARR20 =[1, 2, 3, 4] Arr20[0] =0Arr20[1... 1] =[5, 8]//In array[1] here Insert 5, 82 elements, this function is only valid in the interval state, if a[3] = [5,8] will be an error        println(ARR20)//[0,5,8,3,4]how the//generics are used to define the storage of the container. There is also an abbreviated form of the array, which is more readable, but essentially the same. Now you can't add elements of non-int to the array. It sounds awful, but it's very useful. It is no longer necessary to use the API to record the elements stored in an array that are returned from a method or stored as attributes. You can tell the compiler that this information, the compiler is more intelligent in error checking, and can be optimized earlier. Let arr2: [Int] =[1, 2, 3, 4]//array can only be int typeLet ARR3: [Float] =[1, 2, 2.3]println("ARR3 is \ (ARR3)") Let ARR4: [String] = ["1","CHW","Love"]println("ARR4 is \ (ARR4)")//var variable array is additive, let declaration is not additive, that is, variable and immutable group meaning, in objective-c and Cocoa, you by selecting two different classes (NSString and nsmutablestring ) to specify whether the string can be modified, and whether the string in Swift can be modified is determined only by defining a variable or constant, realizing the unification of multiple types of variability operations.         varArr5:array =[1, 2, 3,"Cheng"] ARR5.Append("CHW")println("ARR5 is \ (ARR5)")//DictionaryLet DIC11 = ["Name":"CHW","Sex": 1,"Height": 175.0]println("This is a dictionary \ (DIC11)")//Swift = There is no return value        varA1 =5        varA2 =5        ifA1 = = A2 {println("===")        }Else{//println ("!==")}//string concatenationLet Cheng ="C"Let Hong ="H"Let Wei ="W"Let Chenghongwei = Cheng + Hong + weiprintln("My name is \ (Chenghongwei)")//Take out surplus        println(8%3)//= 2        println(8%2.5)//8% 2.5 = 8-2.5 * 3 = 0.5 Decimals is also available for redundancy//Swith statements        vari = -        SwitchI Case0... Ten:println("Range is 0-10") Case One...:println("Range is 11-20") Case +..<30:println("Range is [21-30)")default: Break}varName ="CHW"        SwitchName { Case "Cheng":println("Name is Cheng") Case "Hong":println("Name is Hong") Case "Wei":println("Name is Wei")default:println("Someone Else") Break}//Closure probenames = ["Chris","Alex","Ewa","Barry","Daniella"]funcBackwards (s1:string,s2:string)->bool {returnS1&GT;S2}//UI ControlsLet lab = UILabel (frame:cgrectmake( +, -, $, -)) Lab.text ="Hello Chw"Lab.backgroundcolor = Uicolor.greencolor () lab.textalignment = Nstextalignment.center Self.view.addSubview (l AB) Let btn = UIButton (Frame:cgrect (origin:cgpointmake( +, the), Size:cgsizemake( +, the)) Btn.backgroundcolor = Uicolor.graycolor () btn.settitle ("CHW btn", ForState:UIControlState.Normal) Btn.addtarget (Self, Action:"Btnclick:", ForControlEvents:UIControlEvents.TouchUpInside) Self.view.addSubview (BTN)}//todo:---    funcBtnclick (sender:uibutton!) {println("CHW 111")varAlert = Uialertview () Alert.title ="CHW"Alert.delegate = Self Alert.message ="This is a alert!"Alert.addbuttonwithtitle ("Cancle") Alert.addbuttonwithtitle ("OK") Alert.show ()}//mark:-//mark:---alertviewdelegate    funcAlertview (Alertview:uialertview, Clickedbuttonatindex buttonindex:int) {println("Buttonindex:\ (buttonindex)")ifButtonindex = =0{println("Cancle Btn!")        }Else ifButtonindex = =1{println("Sure Btn")        }    }//mark:-OverridefuncDidreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources, can be recreated.}}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Some swift small knowledge points of collation

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.