Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Protocol Awesome

Johnlin
February 23, 2016

Protocol Awesome

Johnlin

February 23, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. UITableViewDataSource func numberOfSectionsInTableView(tableView: UITableView) -> Int { } func tableView(tableView:

    UITableView, numberOfRowsInSection section: Int) -> Int { } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { }
  2. UITableViewDataSource class LiveTableSource:NSObject, UITableViewDataSource { lazy var items:[String] = ["a","b","c"]

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return items.count }
  3. UITableViewDataSource func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let ident = "cell" let cell = tableView.dequeueReusableCellWithIdentifier(ident) ?? UITableViewCell(style: .Default, reuseIdentifier: ident) let item = items[indexPath.row] cell.textLabel?.text = item return cell }
  4. UITableViewDataSource var tableVC = UITableViewController(style: .Plain) var ltc = LiveTableSource()

    tableVC.tableView.dataSource = ltc XCPlaygroundPage.currentPage.liveView = tableVC
  5. Protocol protocol Animal : Creature { var age:Int { get

    } var name:String {get set} func move() }
  6. SuperTextView class SuperTextView: UITextView { func append(s:String) { text.appendContentsOf("\(s)\n") }

    } let superTextView = SuperTextView() superTextView.append("Hello world")
  7. SuperTextView class SuperTextView: UITextView { func append(s:String) { text.appendContentsOf("\(s)\n") }

    func append(u:User) { text.appendContentsOf("\(u.firstName) \(u.lastName) \n") } } let user = User(firstName: "John", lastName: "Lin") superTextView.append(user)
  8. SuperTextView class SuperTextView: UITextView { func append(s:String) { text.appendContentsOf("\(s)\n") }

    func append(u:User) { text.appendContentsOf("\(u.firstName) \(u.lastName) \n") } func append(p:Post) { text.appendContentsOf("\(p.title)\n\(p.content)\n") } } let post = Post(title: "hello", content: "world") superTextView.append(post)
  9. CustomStringConvertible public protocol CustomStringConvertible { /// A textual representation of

    `self`. var description: String { get } } String(5) String(2.333)
  10. User struct User: CustomStringConvertible { let firstName:String let lastName:String var

    description: String { get { return "\(firstName) \(lastName)" } } }
  11. Post struct Post : CustomStringConvertible { let title:String let content:String

    var description: String { get { return "\(title)\n\(content)" } } }
  12. SuperTextView class SuperTextView: UITextView { func append2(any: CustomStringConvertible){ text.appendContentsOf("\(String(any))\n") }

    } let user = User(firstName: "John", lastName: "Lin") let post = Post(title: "hello", content: "world") let superTextView = SuperTextView() superTextView.append2("New") superTextView.append2(user) superTextView.append2(post)
  13. Mixin protocol Swimable { func swim() } extension Swimable {

    func swim(){ "i'm swimming" } } class Duck : Swimable { } Duck().swim() // "i'm swimming"
  14. Loginable Extension extension Loginable { func showLogin() { print("called from

    \(self):please input username & password") } var currentUser:User { get { return User() } } }
  15. User Loginable in ANY UITableViewController extension UITableViewController : Loginable {

    } class CustomTableViewController : UITableViewController { } CustomTableViewController().showLogin()
  16. Comparable • ୞ཁఏڙ < ࿨ == ӡࢉࢠɼबೳⴺࣗಈ㗞ੜ <=, >, >=,

    != struct Ranking : Comparable { let rank:Int } func == (lhs:Ranking, rhs: Ranking) -> Bool { return lhs.rank == rhs.rank } func < (lhs:Ranking, rhs: Ranking) -> Bool { return lhs.rank < rhs.rank } Ranking(rank: 3) <= Ranking(rank: 4) Ranking(rank: 3) > Ranking(rank: 4) Ranking(rank: 3) != Ranking(rank: 4)
  17. Syncable extension extension Syncable { func getFromPath(path:NSURLComponents) -> String {

    return "{\"id\":1,\"name\":\"John\"}" } func postPath(path: NSURLComponents) { print("posting to path:\(path)\n with data:\ (jsonValue)") } mutating func reload() { let json = getFromPath(serverPath) jsonValue = json } func update() { postPath(serverPath) } }
  18. User class User: Syncable, CustomStringConvertible { lazy var name:String =

    "no one" lazy var id:Int = 1 var serverPath: NSURLComponents { get { return NSURLComponents(string: "http://localhost: 3000/users/\(id)")! } } var jsonValue: String { get { return "{\"id\":\(id),\"name\":\"\(name)\"}" } set { print("setting json value to \(newValue)") //set the id & name from json string } } }
  19. Syncable ࢖༻ํ๏ var u = User() u.reload() // setting json

    value to {"id":1,"name":"John"} u.name = “John" u.update() // posting to path:<NSURLComponents 0x7fbd13a01ba0> {scheme = http, user = (null), password = (null), host = localhost, port = 3000, path = /users/1, query = (null), fragment = (null)} with data:{"id":1,"name":"John"}
  20. Q&A