The use of Swift's if-let and guard-let < A look on the wait yo >

Source: Internet
Author: User

////Viewcontroller.swift//Optional options for judging////Created by think Peng on 16/9/16.//copyright©2016 of the year. All rights reserved.//Import UIKitclassViewcontroller:uiviewcontroller {Overridefunc viewdidload () {super.viewdidload ()//demo1 (x:20, Y:nil)Demo3 ()}//MARK:-General wordingFunc Demo (X:int?,y:int?) {        //1. Forced unpacking is riskyPrint (x! + y!)        //2. Use if to determine the direct use if, make the code look ugly        ifX! = Nil && Y! =Nil {print (x! + y!)        }        Else{print ("x or y is nil")        }    }        //MARK:-?? The UseFunc demo1 (x:int?,y:int?) {            //Remember to enclose the brackets.Print (x??0) + (y??0))// -Let name:string? =Nil Print ((name??"") + ("Hello"))//Hello//Note:?? Low priority levelPrint (name??""+"Think")//Think    }        //MARK:-Use of If Let/varfunc Demo2 () {Let username:string? ="Think"Let age:int? = -                //determines whether the value of a variable is nil, has a value into the branch, does not require unpacking        if varName = Username,age =age{name="come on" Age= -Print (name+String (age)} Else{print ("name or the age of nil")        }    }    //MARK:-Use of guard let//Guardian has value, and if let is opposite, logical code decreases the hierarchy of branches lessfunc Demo3 () {Let username:string? ="Think"Let age:int? = -Guard Let name= Username,oage = AgeElse{print ("name or the age of nil")            return; } Print (Name+String (age)}}

The use of Swift's if-let and guard-let < A look on the wait yo >

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.