Swift

Swift Day02 流程控制 函数

2020-11-22  本文已影响0人  望穿秋水小作坊

一、流程控制

1. Swift 中的 if-elseOC 中的有什么区别?(至少说三个点)
let age = 4
if age >= 22 {
    print("Get married")
} else if age >= 18 {
    print("Being a adult")
} else if age >= 7 {
    print("Go to school")
} else {
    print("Just a child")
}
2. 为什么 Swift 中要除去 --++ 这两种运算符?
// 就像下面的代码,result 的值其实很难猜测,并且结果取决于编译器的解析顺序
var age = 10
var result = ++age + ++age
3. Swift 中的 repeat-while?
4. for 的闭区间运算符?
//1. 基本用法
let names = ["Anna", "Alex", "Brian", "Jack"]
for i in 0...3 {
    print(names[i])
}
//输出:Anna Alex Brian Jack

//2. i 默认是 let,有需要时可以声明为 var
for var i in 1...3 {
    i = i + 3;
    print(i)
}
//输出: 4 5 6

//3. 区间的头尾可以使用变量
let a = 0
var b = 3
for i in a...b {
    b = b - 1
    print(names[i])
}
//输出:Anna Alex Brian Jack
print(b)
//输出:-1
5. for 的半开区间运算符?
for i in 1..<5 {
    print(i)
}
//输出:1 2 3 4
6. Swift 中的 switch 和 OC 中的有什么区别?(至少说两点)
var number = 1
switch number {
case 1:
    print("Number is 1")
case 2:
    print("Number is 2")
default:
    print("Number is other")
}
// 输出:Number is 1
7. Swift 中 switchcase 如果需要贯穿效果怎么做?
var number = 1
switch number {
case 1:
    print("Number is 1")
    fallthrough
case 2:
    print("Number is 2")
default:
    print("Number is other")
}
// 输出:Number is 1
// 输出:Number is 2
8. switch 中的一维匹配,使用区间匹配
var counter = 5
switch counter {
case 0:
    print("none")
case 1..<5:
    print("a few")
case 5..<10:
    print("several")
default:
    print("many")
}
//输出:several
9. switch 中的二维匹配,使用元组匹配
let point = (1, 1)
switch point {
case (0, 0):
    print("the origin")
case (_, 0):
    print("on the x-axis")
case (0, _):
    print("on the y-axis")
case (-2...2, -2...2):
    print("inside the box")
default:
    print("outside the box")
}
//输出:inside the box
10. where 一般在什么情况下使用?
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0;
for num in numbers where num > 0 {
    sum += num
}
print(sum)
//输出:60

二、函数

1. 函数的形参返回值的特点
//1.带形参和返回值的函数
func sum(v1: Int, v2: Int) -> Int {
    return v1 + v2
}
sum(v1: 10, v2: 20)

//2.无返回值,无参数的函数
func sayHello() {
    print("Hello")
}
sayHello()
2. 如果函数要实现多返回值怎么做?
func calculate(v1:Int ,v2:Int) -> (sum: Int, difference: Int, average: Int) {
    let sum = v1 + v2
    return(sum, v1 - v2, sum >> 1)
}
let result = calculate(v1: 20, v2: 10)
result.sum // 30
result.difference // 10
result.average // 15
3. 如何给函数写一个文档注释?
注释
4. 什么是参数标签(Argument Label)?
//1.形参和参数标签一致
func sum1(v1: Int, v2: Int) -> Int {
    v2 + v1
}
sum1(v1: 10, v2: 20)

//2.省略参数标签
func sum2(_ v1: Int,_ v2: Int) -> Int {
    v2 + v1
}
sum2(30, 40)

//3.自定义参数标签(让代码看起来更像句子)
func goToWork(at time:String) {
    print("this time is \(time)")
}
goToWork(at: "08:00")
5. 如何给函数添加默认参数值(Default Parameter Value)?
func check(name: String = "nobody", age: Int, job: String = "none") {
    print("name=\(name), age=\(age), job=\(job)")
}
check(name: "Jack", age: 20, job: "Doctor")
check(name: "Rose", age: 18)
check(age: 10, job: "Batman")
check(age: 15)
6. 如何给函数添加可变参数(Variable Parameter)?
func sum(_ numbers:Int...) -> Int {
    var total = 0;
    for number in numbers {
        total += number
    }
    return total
}
sum(1, 2, 3, 4)
7. 输入输出参数(In-Out Parameter)有什么作用?
func swapValue(v1: inout Int, v2: inout Int) {
    let temp = v1
    v1 = v2
    v2 = temp
}
var value1 = 10
var value2 = 20
swapValue(v1: &value1, v2: &value2)
print("value1=\(value1), value2=\(value2)")
//输出:value1=20, value2=10
8. 函数重载(Function Overload)的规则?和函数重写要区分概念
9. 函数类型(Function Type)由什么构成?
func test(){} // () -> Void 或者 () -> ()

func sum(a: Int, b: Int) -> Int { 
    a + b
}// (Int, Int) -> Int

// 定义变量
var fn:(Int, Int) -> Int = sum
fn(2, 3) // 5, 调用时不需要参数标签
10. 如何把函数类型作为函数参数
func sum(a: Int, b: Int) -> Int {
    a + b
}

func difference(v1: Int, v2: Int) -> Int {
    v1 - v2
}

func printResult(_ methodFunc:(Int, Int) -> Int, v1: Int, v2: Int) {
    print("Result:\(methodFunc(v1, v2))")
}

printResult(sum, v1: 10, v2: 20) // Result:30
printResult(difference, v1: 10, v2: 20) // Result:-10
11. 函数类型作为函数返回值
func next(_ input: Int) -> Int {
    input + 1
}

func previous(_ input: Int) -> Int {
    input - 1
}

func forward(_ forward: Bool) -> (Int) -> Int{
    forward ? next : previous
}
forward(true)(3) // 4
forward(false)(3) // 2
12. typealias 是什么?
typealias Byte = Int8
typealias Short = Int16
typealias Long = Int64
13. 嵌套函数(Nested Function)是什么?
func forward(_ forward:Bool) -> (Int) -> Int {
    func next(_ input: Int) -> Int {
        input + 1
    }
    func previous(_ input: Int) -> Int {
        input - 1
    }
    
    return forward ? next : previous
}

forward(true)(3) //4
forward(false)(3) //2
上一篇下一篇

猜你喜欢

热点阅读