diff --git a/Generics.playground/Contents.swift b/Generics.playground/Contents.swift index ef89622..3ef259b 100644 --- a/Generics.playground/Contents.swift +++ b/Generics.playground/Contents.swift @@ -2,48 +2,57 @@ import UIKit protocol Configurable { associatedtype U - func configure(with data:U) + mutating func configure(with data:U) } -class ArrayVC : UITableViewController where Cell:Configurable { - var data:[Configurable.associatedtype] = [] +struct Element : Configurable { + typealias U = String + + var name: String? + + internal mutating func configure(with data: String) { + name = data + } +} +class ArrayVC : UITableViewController where Cell:Configurable { + + var data = [Element]() + let cellID = "cell" - + override func viewDidLoad() { super.viewDidLoad() tableView.register(Cell.self, forCellReuseIdentifier: cellID) } - + override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data.count } - + override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! Cell cell.configure(with: data[indexPath.row]) return cell } - + } extension UITableViewCell: Configurable { - typealias U = String - - func configure(with data: UITableViewCell.U) { - textLabel?.text = data + typealias U = Element + + func configure(with data: Element) { + textLabel?.text = data.name } } - class MyVC : ArrayVC { - + override func viewDidLoad() { super.viewDidLoad() - data = ["John", "Ben", "Alice"] + data = [Element(name: "John"), Element(name: "Ben"), Element(name: "Alice")] title = "Array View Controller" } - } import PlaygroundSupport diff --git a/Generics.playground/playground.xcworkspace/xcuserdata/icarlitux.xcuserdatad/UserInterfaceState.xcuserstate b/Generics.playground/playground.xcworkspace/xcuserdata/icarlitux.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..5e7f660 Binary files /dev/null and b/Generics.playground/playground.xcworkspace/xcuserdata/icarlitux.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Generics.playground/timeline.xctimeline b/Generics.playground/timeline.xctimeline deleted file mode 100644 index bf468af..0000000 --- a/Generics.playground/timeline.xctimeline +++ /dev/null @@ -1,6 +0,0 @@ - - - - -