About Swift Control flow

Source: Internet
Author: User

Control Flow If
    • There is no concept in the C language in Swift 非零即真
    • must indicate the specific condition of judgment in the logical Judgment truefalse
    • The IF statement condition () can be omitted
    • But {} you can't omit it.
let num = 200if num < 10 {    print("比 10 小")} else if num > 100 { print("比 100 大")} else { print("10 ~ 100 之间的数字")}
Trinocular operation
    • Operations in Swift 三目 maintain the same style as OC
var a = 10var b = 20let c = a > b ? a : bprint(c)

Use trinocular appropriately to make the code more concise

Optional type judgment
    • Because the content of the optional type may be nil , and once is nil not allowed to participate in the calculation
    • Therefore, in practical development, it is often necessary to determine whether the content of an optional type isnil
Single selectable type judgment
Let URL =nsurl (string:  "http://www.baidu.com") //: Method 1: Forcibly unpacking-defect, if the URL is empty, the runtime crashes let request = nsurlrequest (url:url!) //: Method 2: First judgment-the code still needs to use '! ' Force unpack if url! = nil {let request = nsurlrequest (url:url!)} //: Method 3: Use ' If let ', which means that once you enter the IF branch, you are not in the optional type if let u = URL where u.host = " www.baidu.com "{let request = nsurlrequest (url:u)}   
Optional type condition judgment
//: 1> 初学 swift 一不小心就会让 if 的嵌套层次很深,让代码变得很丑陋if let u = url {    if u.host == "www.baidu.com" { let request = NSURLRequest(URL: u) }}//: 2> 使用 where 关键字,if let u = url where u.host == "www.baidu.com" { let request = NSURLRequest(URL: u)}
    • Summary
      • if letCan not be judged with the use && , || equal conditions
      • If you want to increase the condition, you can use the where clause
      • NOTE: where clauses do not have smart hints
Multiple optional types to judge
//: 3> can use ', ' to also determine if multiple optional types are empty let oname: String? = "Zhang San" letsoNo: Int? = + if let na me = oname { if let no = oNo { print ("Name:" + name + "study number:" + String (NO))}}if let Name = Oname, let no = oNo { print ("Name:" + name + "study number:" + String (NO))} /c17> 
The variable needs to be modified after judging
let oName: String? = "张三"let oNum: Int? = 18if var name = oName, num = oNum { name = "李四" num = 1 print(name, num)}
Guard
    • guardis with the if let opposite syntax, the Swift 2.0 launches the
 let oname: string? = " Zhang San "let onum: int? =  18guard let name = Oname else { Print ( "name is empty") return}guard let num = onum else {print ( "num is empty") Span class= "Hljs-keyword" >return}//code execution to this point, name & Num are all valued print (name) print (num)       
    • When the program is written, the code after the conditional detection is relatively complex
    • Benefits of using Guard
      • Be able to judge every single value
      • In the True Code Logic section, a layer of nesting is omitted.
Switch
    • switchNo longer limited to integers
    • switchCan be 任意数据类型 judged against
    • No longer neededbreak
    • caseThere must be statements that can be executed after each one.
    • To ensure that all possible situations are handled, otherwise the compiler will directly error, the conditions can be placed in the default branch
    • casethe variables defined in each are only valid in the current case , while OC requires the use of a{}
 let score =  "excellent" switch score {case " excellent ": let name =  "student" print (name +  "80~100 min") case  "good": print ( "70~80 min") case print ( "60~70 min") case  "difference": print (" failed ") default: break}     
    • Switch can also be used to assign values and use where clauses
Let point =Cgpoint (x:Ten, Y:10)Switch Point {CaseLet Pwhere p.x = =0 && P.y = =0:print ( "center point") case let p where p.x =  0: print ( "y-axis") case let p where p.y =  0: print ( "x axis") case let p where abs (p.x) = = abs (p.y): print ( "diagonal") default: print ( "other")}    
    • If you only want to make conditional judgments, the assignment part can be omitted
switch score {case _ where score > 80: print("优")case _ where score > 60: print("及格")default: print("其他")}

About Swift Control flow

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.