大佬教程收集整理的这篇文章主要介绍了Swift开篇003->控制流、函数,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
PART_A 控制流
for
while
if
if
if ... else
if ... else if ... else
switch-case
case默认不用 break
来结束switch选择
case可用 ,
来包含多个条件
case中可包含区间 ...
、..<
、 ..>
case中可包含元组 case (_,0)
,其中 _
代表所有可能的值
case分支可进行值绑定:case (let x,0)
case中可用 where
语句来判断额外的条件:case let (x,y) where x == y
let today = "Monday"
switch today {
case "Monday":
print("today is Monday")
case "Tuesday":
print("today is Tuesday")
case "Wednesday":
print("today is Wednesday")
case "Thursday","Friday":
print("Don't put")
default:
print("404")
}
// today is Monday
控制转移语句
检测 API 可用性
if #available(iOS 9,OSX 10.10,watchOS 9.7,*) {
//
} else {
//
}
函数参数与返回值
无返回值:func test(str1 : String)
多重返回值:通过元组返回
可选元组返回类型:func test(arr : [Int]) -> (min : Int,max : int)?
func test(arr : [Int]) -> (min : Int,max : int) {
var currentMin = arr[0],currentMax = arr[0]
for value in arr[0] ... arr.count {
if value > currentMax {
currentMax = value
} else if value < currentMin {
currentMin = value
}
}
return (currentMin,currentMaX)
}
默认参数值:func test(num : Int = 12)
可变参数:func test(nums : Double...) -> Double
以上是大佬教程为你收集整理的Swift开篇003->控制流、函数全部内容,希望文章能够帮你解决Swift开篇003->控制流、函数所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。