//: Playground - noun: a place where people can play
import UIKit
/*
* 本节主要内容:
* 1.可变长参数
* 2.函数形参的性质(**)
* 3.函数类型Function Type(***)
* 4.函数类型作为其他函数参数
* 5.函数类型作为其他函数的返回值
*/
print("hello", "world", 18, true)
print("hello", "world", 18, true, separator: "**", terminator: "!")
// 如何声明可变长参数的函数? 验证
// 语法: 参数类型后面添加...
// 备注: 可变长参数只能是一个
func sayHello(_ names: String..., greetings: String) {
for name in names {
print("\(greetings) to \(name)")
}
}
//sayHello("Kara", "Kimi", greetings: "hello")
/*
* 1.值传递: 默认函数的形参是不可变的(常量)
* 2.引用传递: 参数类型前面加上关键词inout
* 3.如果是引进传递, 调用函数, 在参数前面加上取地址符号&
*/
var numberOne = 10
var numberTwo = 100
// swap交换两个数
swap(&numberOne, &numberTwo)
numberOne
numberTwo
// 模拟swap函数, 实现两个数的交换
func reverse(first: inout String, second: inout String) {
// let tmp = first
// first = second
// second = tmp
// 元组
(first, second) = (second, first)
}
var oneString = "hello"
var twoString = "world"
reverse(first: &oneString, second: &twoString)
oneString
twoString
// 函数类型: 每个函数都有一个特定的函数类型, 由函数的参数类型和返回类型组成
func addTwoInts(_ first: Int, _ second: Int) -> Int {
return first + second
}
func multipyTwoInts(_ first: Int, _ second: Int) -> Int {
return first * second
}
// (Int, Int) -> Int
var firstFunctionType = addTwoInts
// (Int, Int) -> Int
var secondFunctionType = multipyTwoInts
// 上述两个变量的类型相同, 称为"函数类型的变量"
// 调用函数, 可以通过变量
firstFunctionType(10, 20)
// 显示声明函数类型的变量
var thirdFunctionType: (Int, Int) -> Int = addTwoInts
// 下面几种方式等价:
// 没有参数,没有返回值的函数类型的变量
var forthFuncType: () -> ()
var fifthFuncType: () -> Void
var sixthFuncType: (Void) -> ()
var seventhFuncType: (Void) -> Void
// 函数类型作为其他函数的参数
var array: [Int] = []
for _ in 0..<10 {
array.append(Int(arc4random()%30))
}
array.sort()
/*
* 需求: 其他的多种排序方式如何实现?
* 1.倒序方式排序
* 2.按照字符串的排序方式
* 3.离15进的, 排在前面
*/
// array.sort(by: <#T##(Int, Int) -> Bool#>)
// 排序算法(函数类型)只能是: (Int, Int) -> Bool
func getBiggerNumber(_ first: Int, _ second: Int) -> Bool {
return second < first
}
array.sorted(by: getBiggerNumber)
func compareString(_ first: Int, _ second: Int) -> Bool {
return String(first) < String(second)
}
array.sorted(by: compareString)
func nearSomeNumber(_ first: Int, _ second: Int) -> Bool {
return abs(first - 15) < abs(second - 15)
}
array.sorted(by: nearSomeNumber)
// 理解: 函数类型作为其他函数的参数
// 声明两个函数: 计算步长
func stepForword(_ input: Int) -> Int {
return input + 1
}
func stepBackword(_ input: Int) -> Int {
return input - 1
}
// 声明函数, 参数Bool, 返回是函数类型(参数类型+返回类型)
func chooseStepFunction(backward: Bool) -> ((Int) -> Int) {
return backward ? stepBackword : stepForword
}
var currentValue = 5
// funtionType: (Int) -> Int
var funtionType = chooseStepFunction(backward: currentValue > 0)
// 执行functionType函数类型
while currentValue != 0 {
print("currentValue is \(currentValue)")
currentValue = funtionType(currentValue)
}
/*
* 课堂练习一: 声明变长参数(类型: Double)函数, 计算并返回传入的参数的平均值.
*/
/*
* 课堂练习二: 给定整型类型Int的数值, 返回该整型值的二进制字符串String
*/
/*
* 课堂练习三: 给定整型数组(全班同学的分数), 将数组中的数据, 按照如下的三种方式进行修改(该分数)
* 1.方式一: 对每个分数开平方*10
* 2.方式二: 对每个分数/150*100
* 3.方式三: 对每个分数+5
* 提示: 使用函数类型; 声明四个函数
*/
Swift 系统学习 12 可变长参数函数
最新推荐文章于 2023-01-06 22:37:45 发布