If you add a second general parameter without restrictions and enter a function to return it, then any return value will be accepted:
extension Array {
func forEach<U>(function: (Element) -> U) {
for e in self {
function(e)
}
}
}
func f(i: Int)->Int {
return i * 2
}
func g(i: Int) -> Double {
return Double(0.0)
}
func h(i: Int) {
println("\(i)")
}
let a = [1,2,3]
a.forEach(g)
a.forEach(f)
a.forEach(h)
However, Id strongly recommends you not to do this and use it instead for…in.
, . ( ), for…in , , .
, . , :
func someFunc(a: [Int]) {
a.forEach { i -> () in
return
}
assert(false)
}
- , - ( ). forEach . , , , forEach, ( , continue ).