高端网站定制站,网络游戏的利弊,视频网站如何做营销策划,网站建设怎么改首页的标题在Swift中#xff0c;有一种变量称为可选变量#xff08;Optional#xff09;#xff0c;具体说明见Swift初步入门。这种变量的值可以存在也可以为空#xff08;nil#xff09;。在Swift中#xff0c;可以通过将if语句和赋值语句结合#xff0c;有条件地展开#xff0…在Swift中有一种变量称为可选变量Optional具体说明见Swift初步入门。这种变量的值可以存在也可以为空nil。在Swift中可以通过将if语句和赋值语句结合有条件地展开unwrap可选变量即在该可选变量值不为nil的前提下展开。这种展开方式称为可选绑定Optional binding。文章Unwrapping Optionals With Optional Binding in Swift - Khawer Khaliq和swift那点事儿之optional(可选绑定)_swift initializer for conditional binding must hav-CSDN博客等都对这种绑定方式做了说明但使用的例子或是涉及类甚至涉及网络爬虫并不是用最简洁的方式介绍可选绑定的使用方式。本文用简单的例子来介绍可选绑定的使用方法。在本文的例子中只使用基本的变量类型。
一、可选绑定的基本介绍
在这里以一个简单的Int?类型的变量开始。
var x:Int? 3
现在设计一个逻辑如果x值不是nil就将其转化为二进制否则不要转。不使用这样的句式
if x ! nil {}
可以这样写
var x:Int? 3
var binStr:String
if let xValue x{ //xValue is the deoptionalized x if x contains valuebinStr String(xValue, radix:2)
}
print(x3 converted to binary is \(binStr))
x nil
if let xValue x{ //non-nilbinStr String(xValue, radix:2)
}
else{ //nilprint(xnil is nil now)
}
在该代码中if let xValue x表明如果x不为nil那么就会执行xValue x语句并给出true的判断结果进入if分支如果x为nil就直接进入else分支。注意xValue在这里不是可选变量而是已被展开的x。
运行结果为
x3 converted to binary is 11
xnil is nil now
注意let xValue x不要加括号以下写法是非法的
if (let xValue x){}
其实还有一种更简单的写法
let y:Int? 10
//a more easy way
if let y{binStr String(y,radix: 2)print(y10 converted to binary is \(binStr))
}//here y is temporarily used as a non-optional inside the if statement
print(now y is still \(y))//see it is still optional. Remember that in optional binding the non-optional variable is temporary
在该代码中如果y值不为nil那么y就会自动转化为正常的、非可选的Int类型进入if分支。也就是说在语句binStr String(y,radix: 2)中y是Int而非Int?类型。但是离开if分支后y会恢复为Int?类型。所以运行结果为
y10 converted to binary is 1010
now y is still Optional(10)
二、一个简单但有用的例子
如果我们要把字符串用于数字运算上肯定要先把它转化为数值类型。但是并不是所有的字符串都能转化为数值类型的。Int()是一个转化的函数但结果也是可选的Int?类型转化失败就输出nil。所以我们可以用可选绑定来用简单的语句设计程序。
可以这样写
if let aa Int(1), let bb Int(2), aa bb{print(aa bb)
} //This is a way of writing several optional binds into an if statement (use comma)
注意在Swift中可以用逗号comma分割不同的条件判断语句。可以阅读这篇文章Swift条件判断中的逗号_swift if判断 性能 ,-CSDN博客。
所以在该代码中Int(1)Int(2)输出的都是Int?类型但只要值不为nil转化成功变量aa和bb都是Int类型可以参与aabb的数字判断。
运行结果为
aa bb
还可以这样写
let aa:Int? Int(1)
let bb:Int? Int(rr)
if let aa, let bb, aa bb{print(aa bb)
}
else{print(At least one of them is not a number so not comparable)
}//this writing is ok too运行结果为
At least one of them is not a number so not comparable 文章转载自: http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.wwklf.cn.gov.cn.wwklf.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.yfmlj.cn.gov.cn.yfmlj.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.krzrg.cn.gov.cn.krzrg.cn http://www.morning.tnthd.cn.gov.cn.tnthd.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.qyglt.cn.gov.cn.qyglt.cn http://www.morning.fwkpp.cn.gov.cn.fwkpp.cn http://www.morning.kzcz.cn.gov.cn.kzcz.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.ntlxg.cn.gov.cn.ntlxg.cn http://www.morning.fwqgy.cn.gov.cn.fwqgy.cn http://www.morning.lqchz.cn.gov.cn.lqchz.cn http://www.morning.trfh.cn.gov.cn.trfh.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.sjwiki.com.gov.cn.sjwiki.com http://www.morning.kwqcy.cn.gov.cn.kwqcy.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.qcbhb.cn.gov.cn.qcbhb.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.pnntx.cn.gov.cn.pnntx.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.nggry.cn.gov.cn.nggry.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.cfnht.cn.gov.cn.cfnht.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.beiyishengxin.cn.gov.cn.beiyishengxin.cn http://www.morning.bqwnp.cn.gov.cn.bqwnp.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.zshuhd015.cn.gov.cn.zshuhd015.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.yxbrn.cn.gov.cn.yxbrn.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.qkrzn.cn.gov.cn.qkrzn.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.tllws.cn.gov.cn.tllws.cn http://www.morning.rkdnm.cn.gov.cn.rkdnm.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.pxbky.cn.gov.cn.pxbky.cn http://www.morning.lsnnc.cn.gov.cn.lsnnc.cn http://www.morning.lcjw.cn.gov.cn.lcjw.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn