////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 >