函数类型
函数类型由参数类型和返回类型组成
func feed()
{
print("feed nothing")
}
func feed(rice: Int) -> Int
{
print("feed rice \(rice)")
return rice;
}
func feed(rice: Int, meat: Int) -> (Int, Int)
{
print("feed rice \(rice) and meat \(meat)")
return (rice, meat)
}
func use_functype()
{
let feed0: () -> Void = feed
let feed1: (Int) -> Int = feed
let feed2: (Int, Int) -> (Int, Int) = feed
feed0();
feed1(5)
feed2(5, 8)
}
output:
feed nothing
feed rice 5
feed rice 5 and meat 8
总结:
- 函数类型的返回类型本质为tuple,因此空tuple可用Void或()表示,但不可omit,只含单个成员tuple括号optional,包含多个成员tuple括号不可omit
- 函数类型的参数类型本质为tuple,因此空tuple可用Void或()表示,但不可omit,只含单个成员tuple括号optional,包含多个成员tuple括号不可omit
- 函数类型在使用上