前端之家收集整理的这篇文章主要介绍了
swift 闭包 由浅入深 优化,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//: Playground - noun: a place where people can play
import UIKit
let animals = ["fish","cat","chicken","dog"]
let sortedString = animals.sort({(one: String,two: String) -> Bool in
return one < two
})
let sortedString2 = animals.sort({(one,two) -> Bool in
return one < two
})
let sortedString3 = animals.sort({(one,two) in
return one < two
})
let sortedString4 = animals.sort({one,two in
return one < two
})
let sortedString5 = animals.sort({one,two in
one < two
})
let sortedString6 = animals.sort({$0 < $1})
let sortedString7 = animals.sort(){$0 < $1}
print(sortedString7)
let sortedString8 = animals.sort{$0 < $1}
print(sortedString8)
let sortedString9 = animals.sort(>)
print(sortedString9)
typealias stateMachineType = () ->Int
func makeStateMachine(maxState: Int) -> stateMachineType{
var currentState: Int = 0
return{
currentState++
if currentState > maxState{
currentState = 0
}
return currentState
}
}
let tt = makeStateMachine(2)
print(tt())
print(tt())
print(tt())
print(tt())
print(tt())
原文链接:https://www.f2er.com/swift/325470.html