Share
Embed
Transcript
48 slides
-
Protocol ਅੋແ༻ • େ֓༗Cocoa[Touch] ࠽။༻౸ሏ • Ṝഐࢠେ֓༗༻౸ UITableViewDelegate & UITableViewDataSource
ࣕቮ • আྃ፤ိᙛdelegate ೭֎ෆಓؐೳװᅊ -
UITableViewDataSource func numberOfSectionsInTableView(tableView: UITableView) -> Int { } func tableView(tableView:
UITableView, numberOfRowsInSection section: Int) -> Int { } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { } -
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 } -
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 } -
UITableViewDataSource var tableVC = UITableViewController(style: .Plain) var ltc = LiveTableSource()
tableVC.tableView.dataSource = ltc XCPlaygroundPage.currentPage.liveView = tableVC -
Protocol protocol Animal : Creature { var age:Int { get
} var name:String {get set} func move() } -
SuperTextView class SuperTextView: UITextView { func append(s:String) { text.appendContentsOf("\(s)\n") }
} let superTextView = SuperTextView() superTextView.append("Hello world") -
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) -
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) -
CustomStringConvertible public protocol CustomStringConvertible { /// A textual representation of
`self`. var description: String { get } } String(5) String(2.333) -
User struct User: CustomStringConvertible { let firstName:String let lastName:String var
description: String { get { return "\(firstName) \(lastName)" } } } -
Post struct Post : CustomStringConvertible { let title:String let content:String
var description: String { get { return "\(title)\n\(content)" } } } -
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) -
Extension • ՄҎ༻ိमվݱ༗తclass/struct • ૾Obj-C త Category Ұᒬ • ՄҎᩋprotocol
༗༬ઃత implementation -
Extension extension Int { func next() -> Int { return
self + 1 } } 2.next() //3 -
Mixin protocol Swimable { func swim() } extension Swimable {
func swim(){ "i'm swimming" } } class Duck : Swimable { } Duck().swim() // "i'm swimming" -
Loginable Extension extension Loginable { func showLogin() { print("called from
\(self):please input username & password") } var currentUser:User { get { return User() } } } -
Use Loginable in custom UIViewController class LoginView: UIViewController, Loginable {
} let lv = LoginView() lv.showLogin() lv.currentUser -
Use Loginable in custom UIButton class LoginButton : UIButton, Loginable
{ } let lb = LoginButton() lb.showLogin() -
User Loginable in ANY UITableViewController extension UITableViewController : Loginable {
} class CustomTableViewController : UITableViewController { } CustomTableViewController().showLogin() -
Use Loginable on Int (㟩) extension Int : Loginable {}
5.showLogin() -
Type Class • հ໘Mixin తࠞ߹ᱪ • ཁClient Class ఏڙ෦㟨functionɼबೳఏڙڐଟ ᔤੜޭೳ
• ៃయൣྫ: Comparable -
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) -
Syncable protocol Syncable { var serverPath:NSURLComponents{ get } var jsonValue:String
{ set get } mutating func reload() func update() } -
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) } } -
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 } } } -
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"} -
Protocol ༻ • ՄҎ፤ိᙛ࡞௨༻తߔ௨ಓ • ՄҎ༗༬ઃመ࡞ɼఏڙଟॏঝൠతᏈՌ • ՄҎᩋ㟬తఔࣜߋ؆ܿɼߋ༰қमվɼߋෆ༰қग़ࡨ • Protocol
> ঝɼҰݸClass ೳঝҰݸ Classɼ ୠੋՄҎ༗ଟ protocol