I. Constants and variables in Swift
/*Define constants and variables in Swift with Let/varlet to define a constant Var represents the definition of a variable the definition of constants and variables in Swift does not require write data types, and compiler-Hui automatically deduces swift development techniques based on the true type of data behind us: In swift development, we often use the OPTION + click Key to view methods or variables **///ConstantsLet num =10.1//num = 9.0//constants cannot be modified//variables//Note: You may not write a semicolon after each statement in SWIFT development, but you will not be able to write an error//If you have more than one statement on the same line, you must write a semicolon after the daily statementvar value =Tenvalue= One//equivalent to NSLog in OCPrint (value)
Two. Data types in Swift
/* There is no implicit type conversion in swift, all type conversions must be displayed */ Ten 9.9 = num1 + = Double (NUM1) + num2// Swift is strong language/oc is weak language / /// error = Int (10.1 )
Three. Branches in Swift
/*if use in Swift is basically the same as OC in 1.Swift if you can omit () 2.Swift even if there is only one statement after the IF, you cannot omit {}3. In C and OC, there is a concept of 0 that is true in swift, the condition can only put bool value, Go to the value of only two True/false*/Let num5=Tenif(NUM5 = =Ten) {print ("OK")}ifNUM5 = =Ten{print ("OK")}/*The switch1 in Swift can be omitted in the following () 2. Switch in OC If no break will penetrate, but OC does not, Swift does not penetrate 3.OC if you want to define the variable in case again, you must add {} to determine the scope, but not 4 in Swift. The default position in OC can be written casually, and only if all the case is not satisfied will execute default and the default in Swift can only be placed in the last 5. The default in OC can be omitted, and "most" of Swift cannot be omitted*/Switch(NUM5) { Case 1: Print ("1") Break Case 2: Print ("2") Break Case Ten: Print ("Ten") Breakdefault: Print (" Other") Break}
//loops in Swift/*normal for loop and OC are basically consistent*///0~9 forvar i =0; I <Ten; i++{print (i)}//Swift Featured Loops//The 0..<10 represents an interval range, starting from 0 to 9 contains the head does not contain the tail forIinch 0.. <Ten{print (i)}//_ means ignore, if you do not care about a parameter, you can use _//in Swift, _ Very high frequency of use for_inch 0.. <Ten{print ("LNJ")}//The 0...10 represents an interval range, starting from 0 to 10 with the end containing the head and the tail forIinch 0...Ten{print (i)}/*Swift's while loop is almost the same as in OC, and is seldom used while in development*/var a=0 whileA <Ten{print (a) a++}/*when Swift is upgraded to 2.0, the Do-while loop has undergone a great change do and does not do because do is used as a catch exception*/var b=0repeat{print (b) B++} whileb<Ten
Let NUM6 =TenifNUM6 = =5{print ("5")}Else{print ("Ten")}/*in OC, if else can use the three-mesh operator shorthand NOTE: The trinocular operator is used very much in the development of Swift*/Let res= (NUM6 = =5) ?5:TenPrint (res)/*Optional Type: Represents can have or can not optionalconvenience init? After the SLR sees a method or data type behind it?, the representative returns an optional type using optional types to be aware, if you print the optional type directly, Then the printed value will be optional wrapped! The delegate tells the compiler that there must be a value in the optional type, forcing parsing if there is no value in the optional type and forced parsing, the program crashes*/Let URL= Nsurl (string:"http://baidu.com") print (URL) print (URL!) Let Url2= Nsurl (string:"http://baidu.com/Pictures") print (URL2)//Swift's intention is to allow us to compile most of the runtime errors and to resolve them//Let request=nsurlrequest (url:url!)ifURL! =nil{Let request= Nsurlrequest (url:url!)}//Optional Bindings//The value of the URL is fetched to assign to the urlfinal variable, and if the value is not nil, then you can enter {}ifLet urlfinal =url{Let request=nsurlrequest (url:urlfinal)}
Swift Learning-constants-variables-use of data types (i)