我写了一个函数,它通过迭代给定UIView的superviews来获取对特定UIView子类(在这种情况下为UITableView)的引用。使用命令式风格可以正常工作,但似乎这是一个非常适合“铁路导向编程”的问题。由于我还在学习函数式编程,是否有人能够建议一个更优雅的函数式版本?
func findTableView(var view: UIView) -> UITableView? {
var table: UITableView? = nil
while table == nil {
guard let superView = view.superview else { return nil }
view = superView
table = view as? UITableView
}
return table
}