swift - Modifying a class to display specific output -


i new swift , having bit of trouble bit in particular. attached code need run part of project. details @ end.

 class screen:datasource {     var items:[string]=[]     func run(){         let lv = tableview()         items = ["john","paul","george","ringo"]         let ds = self         lv.datasource=ds         lv.drawlist()     } }  class tableview {     // class displays list given datasource     var datasource:datasource?     init (){         self.datasource=nil     }      func drawlist(){         for(var i=0;i<datasource!.getsize();i++) {             print("\(datasource!.getitem(at:i))")             print("--------------------------")         }     } }  protocol datasource {     func getsize()->int     func getitem(at pos:int)->string }  let screen = screen() screen.run() 

without changing "run" function/method, need have print this:

john -------------------------- paul -------------------------- george -------------------------- ringo -------------------------- 

i'm not sure modify in screen class. have far:

class screen: datasource {          var items: [string]=[]          func run() {                   let lv = tableview()                   items = ["john","paul","george","ringo"]                   let ds = self                   lv.datasource=ds                   lv.drawlist()          }      //need here } 

any tips appreciated.

use extension make screen conform datasource:

 class screen {     var items:[string]=[]     func run(){         let lv = tableview()         items = ["john","paul","george","ringo"]         let ds = self         lv.datasource=ds         lv.drawlist()     } }  extension screen: datasource {     func getsize() -> int { return items.count }     func getitem(at index:int) -> string { return items[index] } } 

you put datasource conformance , methods on class itself, doing in extension common style organizing class's members.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -