一.Swift函数的基本写法
func sayHello(name:String) -> String {
let result = "Hello," + name + "!"
return result
}
sayHello(name: "Sunshine") // 函数返回的结果是:Hello,Sunshine!
// 函数名:sayHello
// 参数列表:name - 参数名 String - 参数类型
// 返回值:String - 返回值的参数类型
因为 name
有可能为nil
,所以函数的name
应该是可选类型:
func sayHello(name:String?) -> String {
let result = "Hello," + (name ?? "Guest") + "!"
return result
}
var nickname:String?
nickname = "Sunshine"
print(sayHello(name: nickname))
二.使用元组返回多个值
// 返回值不一定要给返回的名字的 但是还是建议加上 增强代码的可读性
var userScores:[Int] = [12,999,666,7687,9999,1024,2567]
func maxmainScores(scores:[Int]) -> (maxScores:Int, mainScores:Int)? {
// 在实际项目中 读取到的数组userScores可能为nil 所以函数返回的元组也是一个可选类型 如果数组为nil,就return nil
if scores.isEmpty {
return nil
}
var maxScore = scores[0], minScore = scores[0]
for score in scores {
maxScore = max(maxScore, score)
minScore = min(minScore, score)
}
return(maxScore,minScore)
}
print("测试 - \(maxmainScores(scores: userScores))") // 打印的结果:测试 - (maxScores: 9999, mainScores: 12)
Q:在实际开发中可能会遇到userScores没有数据就返回nil的情况,该如何改进程序?
var userScores:[Int]? = [12,999,666,7687,9999,1024,2567]
userScores = userScores ?? []
func maxmainScores(scores:[Int]) -> (maxScores:Int, mainScores:Int)? {
if scores.isEmpty {
return nil
}
var maxScore = scores[0], minScore = scores[0]
for score in scores {
maxScore = max(maxScore, score)
minScore = min(minScore, score)
}
return(maxScore,minScore)
}
print("测试 - \(maxmainScores(scores: userScores!))") // 打印的结果:测试 - (maxScores: 9999, mainScores: 12)
三. 参数的默认值
// 在参数类型后面加上: ‘ ="Hello"’表示greeting这个参数默认值为Hello 调用的时候去掉greeting参数即可。
func sayHello(name:String,greeting:String = "Hello",others:String) -> String {
let result = greeting + "," + name + others + "!"
return result
}
print("测试 - \( sayHello(name: "Sunshine", others: " And Candy"))")
四.可变参数
// Others:Int...:表示可以传任意多个参数 一个函数只能有一个可变参数
func add(a:Int, b:Int, Others:Int...) -> Int {
var result = a + b
for number in Others {
result += number
}
return result
}
var result0 = add(a: 1, b: 2, Others: 3,4,5)
var result1 = add(a: 1, b: 2)
print("测试 - \(result1)")
五.常量参数、变量参数和inout参数
Q:如何把十进制的数据转化成二进制的数据,并且以字符串的形式打印出来?
func toBinary(num:Int) -> String {
var num = num
var result:String = ""
while num != 0 {
result = String(num%2) + result
num = num/2
}
return result
}
let result = toBinary(num: 6)
print("测试 - \(result)") // 打印结果是:110
Q:如何交换两个变量?
func swapTwoInts( a:inout Int, b:inout Int) {
var c = a
a = b
b = c
}
var x = 10, y = 8
swapTwoInts(a: &x, b: &y)
print("测试 - \(x,y)") // 最后的打印结果:测试 - (8, 10)
// inout:告诉系统我传入的参数不是要存入副本的 而是实实在在改变它的值的 相当于C语言里面的址传递
等价于:
var x = 10, y = 8
swap(&x, &y)
print("测试 - \(x,y)") // 最后的打印结果:测试 - (8, 10)
六.函数类型
func add(a:Int, b:Int) -> Int {
return a + b
}
let anotherAdd:(Int,Int) -> Int = add // 让一个常量为函数类型
print("测试 - \(anotherAdd(Int(4.0), 6))")
func sayHello(nickName:String){
print("Hello," + nickName)
}
let anotherSayHello:(String)->() = sayHello
anotherSayHello("Sunshine")
func sayHi(){
print("Sunshine")
}
let anotherSayHi:()->() = sayHi
anotherSayHi()
Q: 如何定义函数的类型?
// 有参数值 有函数有返回值
let anotherAdd:(Int,Int) -> Int = add
// 有参数值 没有函数返回值 ->()不能省略
let anotherSayHello:(String)->() = sayHello
//没有参数值 没有函数返回值
let anotherSayHi:()->() = sayHi
//没有参数值 没有函数返回值
let anotherSayHi:()->Void = sayHi
栗子:
func changeScores(op:(Int) -> Int,scores:inout [Int]) {
for i in 0..<scores.count {
scores[i] = op(scores[i])
}
}
func op1(x:Int)->Int {
return Int(sqrt(Double(x))*10)
}
func op2(x:Int)->Int {
return Int(Double(x)/150.0 * 100.0)
}
func op3(x:Int)->Int {
return x + 3
}
var scores1 = [36,61,78,89,99]
changeScores(op: op1, scores: &scores1)
print("测试 - \(scores1)") // 测试 - [60, 78, 88, 94, 99]
var scores2 = [88,99,108,128,150]
changeScores(op: op2, scores: &scores2)
print("测试 - \(scores2)") // 测试 - [58, 66, 72, 85, 100]
var scores3 = [59,60,77,87,95]
changeScores(op: op3, scores: &scores3)
print("测试 - \(scores3)") // 测试 - [62, 63, 80, 90, 98]
七.闭包的基本语法
var arr = [1,3,5,7,9,8,10,0]
arr.sort() // 无返回值 此时的arr已经按照从小到大的顺序排列
arr.sorted() // 有返回值 返回的结果即是从小到大的顺序排列之后的数组
// 闭包的写法
{(a:Int,b:Int) -> Bool in
// 具体要执行的代码 Bool表示返回值
}
栗子:
var strArr = ["d","cd","bcd","abcd","ab","a"]
strArr.sort { (str1, str2) -> Bool in
if(str1.characters.count != str2.characters.count) {
return str1.characters.count < str2.characters.count
}
return str1 < str2
}
print("测试 - \(strArr)") // 打印结果:测试 - ["a", "d", "ab", "cd", "bcd", "abcd"]
八.使用闭包简化语法
var arr = [1,3,5,7,9,8,10,0]
func compareTwoInts(a:Int,b:Int) -> Bool {
return a > b
}
//arr.sort { (a, b) -> Bool in return a > b}
//print("测试 - \(arr)")
// 简化写法1:(闭包里面的逻辑只有一句话的时候才可以这么省略)
arr.sort { a, b in return a > b}
print("测试 - \(arr)")
// 简化写法2:
arr.sort { a, b in a > b}
print("测试 - \(arr)")
九.值类型和引用类型
func tryToChangeValue( x :Int) {
var x = x
x += 1
}
var num = 6
tryToChangeValue(x: num) // x = 6 没发生变化 说明是值类型
引用类型:function(函数)/closure(闭包)
func calcTotalMiles(todayMiles:Int) -> () -> Int {
var totalMiles = 0
return {
totalMiles += todayMiles
return totalMiles
}
}
栗子:
var dailyTwoMiles = calcTotalMiles(todayMiles: 10)
dailyTwoMiles() // 10
let result = dailyTwoMiles() // 20
print("测试 - \(result)") // 20
var myplan = dailyTwoMiles
let myresult1 = myplan()
print("测试 - \(myresult1)")// 30
dailyTwoMiles()
let myresult2 = myplan()
print("测试 - \(myresult2)")// 50 myplan发生了改变 说明是引用类型
十.枚举的基本用法
enum gameEnding {
case Win
case Lose
case Draw
}
var yourScore = 100
var enemyScore = 100
var theGameEnding:gameEnding
if yourScore > enemyScore {
theGameEnding = gameEnding.Win
}else if yourScore == enemyScore {
theGameEnding = .Draw
}else {
theGameEnding = .Lose
}
switch theGameEnding {
case .Win:
print("测试 - Win")
case .Lose:
print("测试 - Lose")
case .Draw:
print("测试 - Draw")
}
十一.枚举类型关联默认值
enum Months:Int {
case Jan = 1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
}
let curMonth:Months = .Nov
let rawValue = curMonth.rawValue
print("测试 - \(rawValue)") // 11
let nextMonth:Months? = Months.init(rawValue: 12)
print("测试 - \(nextMonth!.rawValue)") // 12
十二.更加灵活的枚举使用方式:枚举里面可以存储不同类型的值
enum barCode {
case UPCA(Int,Int,Int,Int)
case QRCode(String)
}
let proCodeA = barCode.UPCA(4, 102, 345, 6)
let proCodeB:barCode = .QRCode("This is a informaton")
switch proCodeA {
case .UPCA(let number1, let number2, let number3, let number4):
print("测试 - \(number1)\(number2)\(number3)\(number4)")
default:
print("测试 - This is an information")
}