Skip to content

Instantly share code, notes, and snippets.

@coryalder
Last active August 29, 2015 14:12
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 coryalder/b5405b9abac79013c791 to your computer and use it in GitHub Desktop.
Save coryalder/b5405b9abac79013c791 to your computer and use it in GitHub Desktop.
Solving Protocol issue using an abstract class
// Playground - noun: a place where people can play
import UIKit
class MyItem { /* some model properties */ }
protocol ItemViewProtocol: class {
// some protocol methods (e.g. updateWithItem(), etc)
func setupItem(item: MyItem)
init(String)
}
class ItemView: UIView, ItemViewProtocol {
required init(_: String) {
super.init()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupItem(item: MyItem) {
}
}
class SpecificItemView: ItemView {
override func setupItem(item: MyItem) {
// do item setup
}
}
class AnotherItemView: ItemView {
override func setupItem(item: MyItem) {
// do DIFFERENT item setup
}
}
class MyViewController: UIViewController {
var itemView: ItemView = SpecificItemView("ok") as ItemView
override func viewDidLoad() {
itemView.setupItem(MyItem())
itemView.removeFromSuperview()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment