说你有
class Fancy:UIView
Run Code Online (Sandbox Code Playgroud)
你想找到所有兄弟姐妹的Fancy意见. 没问题 ......
for v:UIView in superview!.subviews
{
if let f = v as? Fancy
{ f.hungry = false }
}
Run Code Online (Sandbox Code Playgroud)
所以,尝试扩展,
public extension UIView
{
internal func fancySiblings()->([Fancy])
{
return (self.superview!
.subviews
.filter { $0 != self }
.flatMap { $0 as? Fancy }
)
}
}
Run Code Online (Sandbox Code Playgroud)
太棒了,你现在可以
for f:Fancy in self.fancySiblings()
{ f.hungry = false }
Run Code Online (Sandbox Code Playgroud)
太棒了.
但,
所以,像......
public extension UIView
{
internal func siblings<T>( something T )->([T]) …Run Code Online (Sandbox Code Playgroud)