携手创作,一起生长!这是我参加「日新方案 8 月更文挑战」的第18天,点击检查活动详情

最近在学习移动端开发,记载一下以前端视角学习 IOS 开发的过程。这是我整理了 Swift4 的学习笔记。

函数基本运用

关键字:func
func hello(name:String) ->String
{
    let result =  "Hello,"+name
    return result
}
hello(name: "imagine")

可选型:

func hello(name:String?,greet:String) ->String
{
    let result =  greet+","+(name)!
    return result
}
var nickname:String?  //nil
nickname = "imagine"
hello(name: nickname,greet: "Good Night") //Good Night,imagine"

无参数函数,直接回来一个字符串类型的函数:

func sayHello() ->String
{
    return "Welcome to imaginecode"
}
sayHello() //"Welcome to imaginecode"

空类型void / () ,不回来任何值

func sayVoid() ->Void{
    print("it is a void func")
}

运用元组让函数回来多个值

func maxminScores( scores:[Int]) -> ( maxscore:Int,minscore:Int)? //元组的可选型
{
    if scores.isEmpty{
        return nil
    }
    var curmax = scores[0] ,curmin = scores[0]
    for score in scores[1..<scores.count]{
        curmax = max(curmax, score)
        curmin = min(curmin, score)
    }
    return (curmax,curmin)
}
var scores:[Int]? = [12,60,71,81,91,100] //可选型数组
scores = scores ?? []
if let result = maxminScores(scores: scores!)
{
    print(result.maxscore)
    print(result.minscore)
}

内部参数名和外部参数名

func hello(userName nickname:String,greeting greet:String) -> String{ //内部参数nickname,greet,属于函数体
    let result = greet+":"+nickname
    return result
}
hello(userName: "imagine", greeting: "codeing") //给参数nickname与greet起了外部参数名userName和greeting

参数的默认值

func hello(nickname:String,greet:String = "hello") -> String{ //给greet默认值hello
    let result = greet+":"+nickname
    return result
}
hello(nickname: "imagine") //"hello:imagine"
func hello(nickname:String,greet:String = "hello",other:String = "nihao") -> String{ //给greet默认值hello
    let result = greet+":"+nickname+other
    return result
}
hello(nickname: "imagine",other:"how do you do") //"hello:imaginehow do you do"

可变参数

  • 一个函数最好只能设置一个可变参数,而且该可变参数只能放在这个函数参数列表的最终一个方位
  • 有必要参数 > 默认值参数 > 可变参数
func add(a:Int,b:Int,others:Int ... ) ->Int //others是可变参数 ... 将其解析为数组
{
    var result = a + b
    for num in others
    {
        result += num
    }
    return result
}
var res = add(a: 2, b: 3)
res = add(a: 2, b: 3, others: 4,5,6)
NSLog(format: String, args: CVarArg...) //CvarArg也是可变参数

inout参数 – 引证传递

  • inout用于声明数据是地址传递,也称之为引证传递;
  • inout润饰的参数是不能有默认值的,有范围的参数调集也不能被润饰;
  • 一个参数一旦被inout润饰,就不能再被var和let润饰了。
func swapTwoInts( a:inout Int,b:inout Int)
{
    let t = a;
    a = b
    b = t
}
var x = 0, y = 100
swapTwoInts(a: &x, b: &y) //传入引证参数

函数类型

func add(a:Int,b:Int) -> Int
{
    return a+b
}
let anotherAdd:(Int,Int)->Int = add //参数为两个Int,回来类型为Int  ,add 作为变量
anotherAdd(3,4)
func changeScores1(scores:inout [Int]) {
    for i in 0..<scores.count {
        scores[i] = Int(sqrt(Double(scores[i]))*10)
    }
}
func changeScores2(scores:inout [Int]) {
    for i in 0..<scores.count {
        scores[i] = Int(sqrt(Double(scores[i]))/150.0 * 100.0)
    }
}
func changeScores3(scores:inout [Int]) {
    for i in 0..<scores.count {
        scores[i] += 3
    }
}
var scores1 = [20,40,60,80,90]
changeScores1(scores: &scores1)
var scores2 = [20,40,60,80,90]
changeScores2(scores: &scores2)
var scores3 = [20,40,60,80,90]
changeScores3(scores: &scores3)

改善:

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(sqrt(Double(x))/150.0*100.0)}
func op3(x:Int)->Int {return x + 3}
var scores1 = [20,40,60,80,90]
changeScores(op: op1, scores: &scores1)
var scores2 = [20,40,60,80,90]
changeScores(op: op2, scores: &scores2)
var scores3 = [20,40,60,80,90]
changeScores(op: op3, scores: &scores3)
var arr = [Int]()
for _ in 1...20
{
    arr.append(Int(arc4random()%100))
}
arr
func compareTwoInts(a:Int,b:Int) -> Bool{return a>b }
arr.sort()

回来函数类型的回来值、函数嵌套

//邮费计算
func mailcost1(weight:Int) -> Int
{
    return 1*weight
}
func mailcost2(weight:Int) -> Int
{
    return 2*weight
}
func chooseMailCostMethod(weight:Int) -> (Int)->Int //回来一个Int类型的函数,解耦效果
{
    return weight <= 10 ? mailcost1 : mailcost2
}
func totalCost(price:Int,weight:Int) -> Int
{
    let mailCost:(Int)->Int = chooseMailCostMethod(weight: weight)
    return mailCost(weight) + price*weight
}

另一种写法:函数嵌套

func mailcost1(weight:Int) -> Int
{
    return 1*weight
}
func mailcost2(weight:Int) -> Int
{
    return 2*weight
}
func totalCost(price:Int,weight:Int) -> Int
{
    func chooseMailCostMethod(weight:Int) -> (Int)->Int //函数嵌套
    {
        return weight <= 10 ? mailcost1 : mailcost2
    }
    let mailCost:(Int)->Int = chooseMailCostMethod(weight: weight)
    return mailCost(weight) + price*weight
}