ios - 'self' used before super.init call and property not initialized at super.init call -


i'm trying init array of int in custom uiview:

var graphpoints:[int]  required init?(coder adecoder: nscoder) {     restapimanager.sharedinstance.requestdata() { (json: json) in         item in json[].array! {             let n=item["n"].intvalue             print(n)             if(n>=0) {                 self.graphpoints.append(n)             }         }     }     super.init(coder: adecoder) } 

but int row restapimanager.sharedinstance.requestdata() { reiceved error: 'self' used before super.init call

and @ row super.init(coder: adecoder) error: property self.graphpoint not initialized @ super.init call

swift requires parameters initialized before calling super.init(). there several ways make happen.

declare graphpoints empty initializer this:

var graphpoints:[int] = []   or var graphpoints = [int]() 

you can change graphpoints optional, this:

var graphpoints:[int]? 

you can leave declaration alone, , initialize empty array before calling super.init()

you need move restapi call below super.init call.

hope helps.