func test()
{
print("222")
}
test()
func test1() -> Int{
return 100
}
print(test1())
func test2(name: String) -> String{
return name
}
print(test2(name: "hello world"))
func test3(name:String...){
// print(type(of: name))
for item in name {
print(item)
}
}
test3(name: "a","b")
func test5(name:(n1:String,n2:Int)) -> (String,Int){
var value:(a:String, b:Int)
value.a = name.n1 + " hello"
value.b = name.n2 + 100
return value
}
var value = test5(name: (n1:"world",n2:20))
print(value)
func test1() -> Void
{
print("test1()")
}
var a:() -> Void = test1
a()
//匿名函数
var b:() -> Void = {() -> Void in
print("匿名函数")
}
b()
print("---------------------")
func test2(param:String, param1:Int) -> String
{
return param + String(param1)
}
var c:(String, Int) -> String = test2
let dc = c("hello", 10)
print(dc)
var d:(String, Int) -> String = {(param1: String, param2: Int) -> String in
return param1 + String(param2)
}
let dd = d("王敏",22)
print(dd)
print("---------------------")
func test3(param:[Int]) -> String
{
var temp: String = ""
for item in param {
temp = temp + String(item)
}
return temp
}
var e:([Int]) -> String = test3
var value = e([1,2,3])
print(value)
var f:([Int]) -> String = {(a:[Int]) -> String in
var temp: String = "--"
for item in a {
temp = temp + String(item)
}
return temp
}
//print(f([4,5,6]))
var g:([Int]) -> String = e
print(g([10,11,12]))
func test()
{
print("test()函数,没有参数,没有返回值")
}
var a:() -> Void = test
//a()
func test1(param:()->Void)
{
param()
}
//test1(param: test)
test1(param: {() -> Void in
print("test()函数,没有参数,没有返回值")
})
print("-------------------")
func sum(param:(Int,Int) -> Int) -> Int
{
var value = param(1,2)
print("value=\(value)")
return value
}
func add(a:Int, b: Int) -> Int
{
return a + b
}
//sum(param: add)
var value1 = sum(param: {(a, b) -> Int in
return a + b
})
print("323:\(value1)")
var array = [1,3,5,0,2,8]
func paly2(a:Int, b:Int) -> Bool
{
if a < b {
return true
}else{
return false
}
}
//array.sort(by: {(a, b) -> Bool in
// if a < b {
// return true
// }else{
// return false
// }
//})
array.sort(by: paly2)
print(array)
func paly1(value:Int) -> Int
{
return value * value
}
func play2(value: Int) -> Int
{
return value + value
}
func test(param:Bool) -> (Int) -> Int
{
return param ? paly1 : play2
}
var a = test(param: true)
print(a(3))
print("-----------------------")
func test1(param:Bool) -> (Int) -> Int
{
func play1(value:Int) -> Int
{
return value * value
}
func paly2(value: Int) -> Int
{
return value + value
}
return param ? play1 : paly2
}
var b = test1(param: true)
print(b(4))
var a:() -> Void = {() -> Void in
print("a")
}
print(type(of: a))
var b:() -> Void = {
print("b")
}
b()
print("====================")
//不带参数,不带返回值
func test(parm:() -> Void)
{
parm()
}
test(parm: {print("不带参数不带返回值")})
print("====================")
func test2(param:(Int) -> Void)
{
param(10)
}
test2(param: {(value: Int) -> Void in
print(value)
})
test2(param: {(value) in
print(value)
})
print("--------------")
func test3(param: (Int, Int) -> Int)
{
print(param(10,20))
}
//test3(param: {(item1: Int, item2: Int) -> Int in
// return item1 + item2
//})
test3(param: {$0 + $1})
swift5.0 -- 函数
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、流程控制 1.1、if 语句let age = 5if age > 10 { print("\(age...
- 封装了moya,链式,类AFN式请求 SwiftHttpRequest Github地址 在线转model工具 更...