initialization - Init a class with arguments on swift -
this question has answer here:
i trying init class arguments in swift. want able pass array class. let test = mytestclass(data)
.
code:
init(data: [cgfloat]){ super.init(nibname: "test", bundle: nil) self.data = data } required init?(coder decoder: nscoder) { super.init(nibname: "test", bundle: nil) }
error:
you can solve errors initializing data
before calling super
in init(coder
, call appropriate super
method in init(coder
let data : [cgfloat] init(data: [cgfloat]){ self.data = data super.init(nibname: "test", bundle: nil) } required init?(coder decoder: nscoder) { self.data = [cgfloat]() super.init(coder: decoder) }
alternatively declare data
var data = [cgfloat]()
then can write
init(data: [cgfloat]){ self.data = data super.init(nibname: "test", bundle: nil) } required init?(coder decoder: nscoder) { super.init(coder: decoder) }
in case have call super.init(coder: decoder)
in init(coder
.
Comments
Post a Comment