This is a somewhat common picture, but it is not a very good example. I saw him introduce errors into ObjC projects. He thinks too much about the hierarchy of views and at the same time becomes fragile when it changes (for example, when someone introduces an additional view that you did not expect to control the turns, a true story). The best template is to save a property that points to the SpecificView (or views) that you want to track. Downsizing as a whole is something that should be avoided, not optimized.
However, this is not a scary template, and sometimes it is a very useful pattern. So how can you handle this?
let specifics = self.subviews .filter { $0 is SpecificView } .map { $0 as SpecificView } for view in specifics { ... }
Such a general template, perhaps we can generalize it?
extension Array { func filterByClass<T>(c: T.Type) -> [T] { return self.filter { $0 is T }.map { $0 as T } } } for view in self.subviews.filterByClass(SpecificView) { ... }
However, I think this approach should be avoided when possible, and not overly simplified.
source share