Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rayfix
Created July 1, 2015 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayfix/ea2595e5b1e123e849ec to your computer and use it in GitHub Desktop.
Save rayfix/ea2595e5b1e123e849ec to your computer and use it in GitHub Desktop.
A UIView that conforms
import UIKit
// Rather than a generic type with constraints on being a UIView and conforming to Extra
// you could do something like this.
protocol UIViewConvertable {
var uiview: UIView { get }
}
extension UIView : UIViewConvertable {
var uiview: UIView { return self }
}
protocol Extra {
func special()
}
class SpecialView : UIView, Extra {
func special() {}
}
let m: protocol<UIViewConvertable, Extra> = SpecialView()
@rayfix
Copy link
Author

rayfix commented Jul 1, 2015

Or...

import UIKit

protocol UIViewConvertable {
    var uiview: UIView { get }
}
extension UIView : UIViewConvertable {
    var uiview: UIView { return self }
}
protocol ExtraType {
    func special()
}

protocol SpecialViewType: UIViewConvertable, ExtraType {}

class SpecialView : UIView, SpecialViewType {
    func special() {}
}

let m: SpecialViewType = SpecialView()

@rayfix
Copy link
Author

rayfix commented Jul 1, 2015

Or even more streamlined...

import UIKit

protocol UIViewConvertable {
    var uiview: UIView { get }
}
extension UIView : UIViewConvertable {
    var uiview: UIView { return self }
}

protocol SpecialViewType : UIViewConvertable {
    func special()
}

class SpecialView1 : UIView, SpecialViewType {
    func special() { uiview.backgroundColor = .blackColor() }
}

let m: SpecialViewType = SpecialView1()

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