Skip to content

Instantly share code, notes, and snippets.

@jaclync
Last active May 18, 2017 16:44
Show Gist options
  • Save jaclync/3b75853371dc022f35dd10184e04290b to your computer and use it in GitHub Desktop.
Save jaclync/3b75853371dc022f35dd10184e04290b to your computer and use it in GitHub Desktop.
Multiple type constraints with generics
import UIKit
// Ref: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html#//apple_ref/doc/uid/TP40014097-CH26-XID_292
protocol OptionView: class {
func handleKeyCommand()
}
class QuestionView<V> where V: UIView, V: OptionView {
private let optionView: V
init(optionView: V) {
self.optionView = optionView
optionView.handleKeyCommand()
}
}
class TermOptionView: UIView, OptionView {
func handleKeyCommand() {
print("normal option")
}
}
class DiagramOptionView: UIView, OptionView {
func handleKeyCommand() {
print("diagram option")
}
}
let diagramOptionView = DiagramOptionView()
let question1 = QuestionView(optionView: diagramOptionView)
let termOptionView = TermOptionView()
let question2 = QuestionView(optionView: termOptionView)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment