Last active
May 18, 2017 16:44
-
-
Save jaclync/3b75853371dc022f35dd10184e04290b to your computer and use it in GitHub Desktop.
Multiple type constraints with generics
This file contains 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 | |
// 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