Created
April 5, 2016 12:24
-
-
Save josephlord/7dac28170f06456b436180d7eda6940f to your computer and use it in GitHub Desktop.
Example of requiring object to have or be an object of a certain class and conform to a protocol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class MyView: UIView {} | |
protocol MyProtocol { | |
var view: UIView { get } | |
func myFunc() | |
} | |
extension MyProtocol { | |
func myFunc() { | |
print(self.dynamicType) | |
} | |
} | |
extension UIView { | |
var view: UIView { return self } | |
} | |
extension MyView: MyProtocol {} | |
let myView: MyProtocol = MyView() | |
//myView.frame | |
myView.view.frame = .zero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that if you wanted MyProtocol conformance to apply to things that weren't views you could add an additional protocol that extends it and adds the view property but that isn't shown in this example.