Say what you have
class Fancy:UIView
You want to find all sibling fancy views. No problem ...
for v:UIView in superview!.subviews { if let f = v as? Fancy { f.hungry = false } }
So try the extension,
public extension UIView { internal func fancySiblings()->([Fancy]) { return (self.superview! .subviews .filter { $0 != self } .flatMap { $0 as? Fancy } ) } }
Amazing you can now
for f:Fancy in self.fancySiblings() { f.hungry = false }
Fantastic.
But,
How to generalize this extension to work with any subtype of UIView?
Ideally, can an extension infer a type, even? Like the type?
So, something like ...
public extension UIView { internal func siblings<T>( something T )->([T]) { return (self.superview! .subviews .filter { $0 != self } .flatMap { $0 as? T } ) }
and then you could call it something like this ...
for f in self.siblings(Fancy) for p in self.siblings(Prancy) for b in self.siblings(UIButton)
How can you βtalkβ about the general type extension that is used, for example?
It seems you can "pull it back",
public extension UIView { internal func incredible<T>()->([T]) { return (self.superview! .subviews .filter { $0 != self } .flatMap { $0 as? T } ) } for f:Fancy in self.incredible() for p:Prancy in self.incredible()
Which is surprising, but does not work differently.
You can even ...
self.siblings().forEach{ (f:Fancy) in d.hasRingOn = false }
Therefore, I would still like to know how to "pass" a type similar to for f in self.siblings(Fancy) , and, ideally, even output it as well.
generics ios functional-programming swift swift-extensions
Fattie May 15 '16 at 15:25 2016-05-15 15:25
source share