ios - Whats wrong here: Instance member cannot be used on type -


this question has answer here:

i have following code , i'm confused error message:

instance member 'mydate' cannot used on type 'tableviewcontroller' 

code:

class tableviewcontroller: uitableviewcontroller {         let mydate = nsdate()     let items = [         (1, 9, 7, "a", mydate),         (2, 9, 7, "b", mydate),         (3, 9, 7, "c", mydate),         (4, 9, 7, "d", mydate)     ] 

when write following, can build don't know why oder snippet not working:

class tableviewcontroller: uitableviewcontroller {         let mydate = nsdate()     let items = [         (1, 9, 7, "a", nil),         (2, 9, 7, "b", mydate),         (3, 9, 7, "c", mydate),         (4, 9, 7, "d", mydate)     ] 

the problem here using self before class initialised. can either have getter called every time access variable or compute lazily.

here code:

class tableviewcontroller: uitableviewcontroller {     let mydate = nsdate()     var items : [(int,int,int,string,nsdate)] {         {             return [                 (1, 9, 7, "a", mydate),                 (2, 9, 7, "b", mydate),                 (3, 9, 7, "c", mydate),                 (4, 9, 7, "d", mydate)             ]          }     } } 

lazy computation:

class tableviewcontroller: uitableviewcontroller {     let mydate = nsdate()     lazy var items : [(int,int,int,string,nsdate)] =  {              return [                 (1, 9, 7, "a", self.mydate),                 (2, 9, 7, "b", self.mydate),                 (3, 9, 7, "c", self.mydate),                 (4, 9, 7, "d", self.mydate)             ]       }() } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -