Passing a value into a class to be used when the class is instantiated in Swift -
when creating instance of custom class, want pass value class declaration, used when it's instantiated.
i tried doing through property, doesn't work. correct way achieve this?
(apologies if i'm not wording question quite right, code below makes question clear.)
class hello { let indexinarray: int! override init(frame: cgrect) { super.init(frame: frame) println("this hello number \(indexinarray).") } } index in 0..<4 { let singlehello = hello() singlehello.indexinarray = index }
the desired output:
// hello number 0. // hello number 1. // hello number 2. // hello number 3.
if understand question correctly, you're after:
class hello : hellosuperclass { // note - no longer has declared implicitly unwrapped optional. let index: int // create new designated initialiser takes frame , index arguments. init(frame: cgrect, index: int) { self.index = index super.init(frame: frame) println("this hello number \(self.index).") } } in 0..<4 { let singlehello = hello(frame: cgrect(), index: i) }
Comments
Post a Comment