寒城攻略:Listo 教你 25 天学会 Swift 语言 - 08 Functions

前端之家收集整理的这篇文章主要介绍了寒城攻略:Listo 教你 25 天学会 Swift 语言 - 08 Functions前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

importFoundation


//***********************************************************************************************

//1.Functions(函数

//_______________________________________________________________________________________________

//介绍

/*

函数是用来完成特定任务的独立代码块。给一个函数起一个适合的名字,用来标识函数是做什么的,当函数执行时,调用函数名即可

Swift中的函数语法灵活,可以用来表示任何函数包括从最简单的没有参数名的C风格函数,到复杂带局部和外部参数名的Objective-C风格函数。参数可以提供默认值,以简化函数调用。参数既可以当作传入参数,也可以当作传出参数,也就是说,一旦函数执行结束,传入的参数值可以被修改

Swift中,每个函数都有一种类型,包括函数的参数类型和返回值类型,你可以把函数类型当作任何其他普通类型一样处理,这样就可以把函数当作其他函数的参数,也可以从其他函数中返回函数函数的定义可以写在其他函数的定义中,这样可以在嵌套函数范围内实现功能封装

*/


//2.Defining and Calling Functions(定义和调用函数

//_______________________________________________________________________________________________

//函数的定义和调用:以func作为前缀,返回剪头"->"表示函数的返回类型(func函数(形参) ->返回值类型)

funcsayHello(personName:String) ->String{

letgreeting ="Hello,"+ personName +"!"

returngreeting

}

println(sayHello("Listo"))


//1.Function Parameters and Return Values(函数的参数和返回值)

//函数可以有多个输入参数

funcminusResult(start:Int,end:Int) ->Int{ //函数可以有多个输入参数,在圆括号中用逗号分隔

returnend - start

}

(minusResult(1,10 //无参函数

funcsayHelloWorld() ->String{

return"hello world"

}

(sayHelloWorld())


//无返回值函数(严格的来说,虽然没有定义返回值,sayGoodbye函数依然返回了值,没有定义返回类型的函数会返回一个特殊的值,叫做Void。他其实是一个空的元组,没有任何的元素,可以写成()

funcsayGoodbye(name:String){

println("Goodbye,\(name)")

}

(sayGoodbye //多重返回值的函数(返回元组类型的函数

funccount(string:String) -> (vs:Int) { 定义返回值类型为元组类型

varvowels =0

varconsonants =varothers =forcharacterinstring{

switchString(character).lowercaseString{ 把字符串中的字符全部转化为小写的字符串

case"a","e",27)">"i",27)">"o",27)">"u":

++vowels

case"b":

++consonants

default:

++others

}

}

return(vowels,consonants,others)

}

lettotal =count("some arbitrary string!")

("\(total.vs)vowels and.cs)consonants")


//3.Function Parameter Names(函数的参数名)

//函数参数名称(局部参数名和外部参数名)

funcjoin(s1:String,s2:String{ 此时s1s2joiner都是内部参数名,只能在函数的内部进行使用

returns1 + joiner + s2

}

(join"hello""world"",")) //内部参数名对应的不足是在调用函数的时候,我们没法知道调用函数实参的具体含义


funcjoin1(string s1:使用string,toString,withJoiner三个名称函数内部形参命名作为外部参数名,可以在函数的外部进行使用

returns1 + joiner + s2

}

println(join1(string:"hello",toString:"world",withJoiner:)) 我们在调用函数的时候,使用外部参数名可以更明显的说明实参的含义


//简写外部参数名(当外部参数名和内部参数名相同的时候,可以在局部参数名前面加上"#"来说明这个参数名是外部参数名和局部参数名

funcjoin2(#string:returnstring + withJoiner + toString

}

join2(string:))


//函数参数默认值

funcjoin3(#string:String=) ->{ //在设置函数参数时为参数设置默认值,在函数调用的时候可以不指定函数的实参,当设置默认值时,这个参数自动转化为外部参数名,在调用的时候必须声明外部参数名

join3(string:"-")) 如果在调用函数的时候修改形参默认值,保留修改后的值


//可变参数(当参数的数量不确定时,使用可变参数命名参数即可,一个函数最多能有一个可变参数,可变参数必须在参数的最后位置)

funcairtheticMean(numbers:Double...) ->Double{

vartotal:Double=fornumberinnumbers{

total += number

}

returntotal /Double(numbers.count)

}

(airtheticMean1.23.54.6 //常量参数和变量参数(函数参数默认为常量,如果需要在函数修改参数,就要定义变量参数)

func alignRight(var string:String,count: Int,pad:Character) -> String{ //设置参数 string为变量参数,在函数内部可以修改参数

let amountToPad = count - countElements(string) //使用 "countElements"函数获取字符串长度

let padString = String(pad)

for _ in 1...amountToPad{

string = padString + string

}

return string

}

let originalString ="hello"

let paddedString =alignRight(originalString,10,"_")

println("originalString: " +originalString)

println("paddedString: " +paddedString)


//输入输出参数(变量参数仅仅在函数体内被更改,如果想要一个函数可以修改参数的值,并且想要在函数调用结束之后依旧存在,那么就应定义输入输出参数)

swapTwoInts(inouta:Intb:){ //使用inout函数来定义输入输出参数,输入输出参数不能有默认值,可变的参数不能用inout标记

lettemp = a

a = b

b = temp

}

varsomeInt =3

varanotherInt =8

swapTwoInts(&someIntanotherInt) //调用函数时,当传入的参数作为输入输出参数的时候,在参数前加上&,表示这个值可以被函数修改

("someInt is now),another is now)" //4.Function Types(函数类型)

//函数类型的写法分为三步:1.定义函数2.声明函数类型变量或者常量;3.函数类型变量赋值

funcaddTwoInts(a:Int{ 定义函数

returna + b

}


varmathFunction: (Int 声明函数类型变量


mathFunction=addTwoInts 函数类型变量赋值


"result:\(mathFunction(2,3))") 输出函数类型的变量


//函数类型作为参数类型

funcprintMathResult(mathFun: (Int){

"Result:\(mathFun(a,b)) printMathResult(mathFunction47 //函数类型作为返回类型

funcstepForward(input:Int{

returninput +1

}

funcstepBackward(input:returninput -1

}

chooseStepFunction(backward:Bool) -> ({ //声明chooseStepFunction函数返回函数类型数据,返回类型为(Int) -> Int

backward ?stepBackward:stepForward 返回函数类型

}


varcurrentValue =3

moveNearerToZero =chooseStepFunctioncurrentValue>0) //此时moveNearerToZerostepBackward函数

("moveNearerToZero:moveNearerToZero))" //嵌套函数

funcchooseStepFunction1(backward:Bool) -> ((Int){

funcstepForward1(input:Int{

1

}

funcstepBackward1(input:1

}

backward ?stepBackward1:stepForward1返回函数类型

}

varcurrentValue1 =moveNearerToZero1 =chooseStepFunction1moveNearerToZero1

转载:http://blog.csdn.net/u013096857/article/details/37871237

原文链接:https://www.f2er.com/swift/327686.html

猜你在找的Swift相关文章