Skip to content

Instantly share code, notes, and snippets.

@josephlord
Created April 5, 2016 12:24
Show Gist options
  • Save josephlord/7dac28170f06456b436180d7eda6940f to your computer and use it in GitHub Desktop.
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.
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
@josephlord
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment