2016-04-11 15 views
0

Ben protokol ve bunun gibi uzantıyı"A" ifadesini "A protokolüne uygun" olarak nasıl yapabilirim?

protocol FooProtocol: class { 

} 

extension FooProtocol where Self: UIView { 

    func changeAlphaToZero() { 

     self.alpha = 0 

    } 

} 

ilan etti ve böyle changeAlphaToZero(), kullanmaya çalıştı (sadece bir örnek.)

class MyClass { 

    func setViewAlphaToZeroIfNeeded(view: UIView) { 

     if let v = view as? FooProtocol { 

      v.changeAlphaToZero() // Compile Error "FooProtocol is not a subtype of UIView." 

     } 

    } 

} 

Can Ben view "UIView FooProtocol uygundur etmek döküm "?

cevap

1

ben olması gerektiğini düşünüyorum gibi:

protocol FooProtocol { 
    func changeAlphaToZero() 
} 


extension UIView: FooProtocol { 

    func changeAlphaToZero() { 

     self.alpha = 0 

    } 

} 
İlgili konular