The function can be used to iterate over a sequence (implementing Sequence-protocol).
It returns a dictionary containing either the returned value or a thrown error.
func catchloop<I : Hashable, S: Sequence where S.Iterator.Element == I> | |
(_ range: S, _ f: (I) throws -> Any) -> [I:Any] { | |
var dict = [I:Any]() | |
for i in range { | |
do { | |
let e = try f(i) | |
dict[i] = e | |
} catch let e { dict[i] = e } | |
} | |
return dict | |
} |