首页 技术 正文
技术 2022年11月23日
0 收藏 707 点赞 3,028 浏览 1874 个字

一. 分支的介绍

  • 分支即if/switch/三目运算符等判断语句
  • 通过分支语句可以控制程序的执行流程

二. if分支语句

  • 和OC中if语句有一定的区别

    • 判断句可以不加()
    • 在Swift的判断句中必须有明确的真假
      • 不再有非0即真
      • 必须有明确的Bool值
      • Bool有两个取值:false/true
// 演练一:
let a = 10// 错误写法:
//if a {
// print("a")
//}// 正确写法
if a > 9 {
print(a)
}// 演练二:
let score = 87if score < 60 {
print("不及格")
} else if score <= 70 {
print("及格")
} else if score <= 80 {
print("良好")
} else if score <= 90 {
print("优秀")
} else {
print("完美")
}// 演练三:
// 这个是可选类型,因为只有声明成可选类型后,才可以判断是否为空
// 可选类型会在后续讲解,可先了解即可
let view : UIView? = UIView()// 判断如果view有值,则设置背景
// 错误写法
//if view {
// view.backgroundColor = UIColor.redColor()
//}if view != nil {
view!.backgroundColor = UIColor.redColor()
}

三. 三目运算符

  • Swift 中的 三目 运算保持了和 OC 一致的风格
var a = 10
var b = 50var result = a > b ? a : b
println(result)

四.guard的使用

  • guard是Swift2.0新增的语法
  • 它与if语句非常类似,它设计的目的是提高程序的可读性
  • guard语句必须带有else语句,它的语法如下:
    • 当条件表达式为true时候跳过else语句中的内容,执行语句组内容
    • 条件表达式为false时候执行else语句中的内容,跳转语句一般是return、break、continue和throw
guard 条件表达式 else {
// 条换语句
break
}
语句组
  • 例子
var age = 18func online(age : Int) -> Void {
guard age >= 18 else {
print("回家去")
return
} print("可以上网")
}online(age)

四.switch分支

switch的介绍
  • Switch作为选择结构中必不可少的语句也被加入到了Swift中
  • 只要有过编程经验的人对Switch语句都不会感到陌生
  • 但苹果对Switch进行了大大的增强,使其拥有其他语言中没有的特性
switch的简单使用
  • 基本用法和OC用法一致
  • 不同之处:例子:
    • switch后可以不跟()
    • case后可以不跟break(默认会有break)
let sex = 0switch sex {
case 0 :
print("男")
case 1 :
print("女")
default :
print("其他")
}
  • 简单使用补充:

    • 一个case判断中,可以判断多个值
    • 多个值以,隔开
let sex = 0switch sex {
case 0, 1:
print("正常人")
default:
print("其他")
}
  • 简单使用补充:

    • 如果希望出现之前的case穿透,则可以使用关键字fallthrough
let sex = 0switch sex {
case 0:
fallthrough
case 1:
print("正常人")
default:
print("其他")
}
Switch支持多种数据类型
  • 浮点型的switch判断
let f = 3.14
switch f {
case 3.14:
print("π")
default:
print("not π")
}
  • 支持字符串类型

    • 字符串的使用后面会详细讲解
let m = 5
let n = 10
var result = 0let opration = "+"switch opration {
case "+":
result = m + n
case "-":
result = m - n
case "*":
result = m * n
case "/":
result = m / n
default:
result = 0
}print(result)
switch支持区间判断
  • 什么是区间?

    • 通常我们指的是数字区间:0~10,100~200
  • swift中的区间常见有两种
    • 开区间:0..<10 表示:0~9,不包括10
    • 闭区间:0…10 表示:0~10
let score = 88switch score {
case 0..<60:
print("不及格")
case 60..<80:
print("几个")
case 80..<90:
print("良好")
case 90..<100:
print("优秀")
default:
print("满分")
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,893
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,422
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,240
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,054
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,683
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,720