Antiokhos
Antiokhos

Reputation: 3055

Swift declare class in class

Let me explain my question with an Example.

class Mother: NSObject {
var momVar:Int =5
var subClass : child(mylevel:5)   //  <-- ********    Error  //
init(){
    momVar=1000
    level=1
}

func print(){
    NSLog("%d",momVar);
}

func subMethod(){
    subClass =child(myVar: 5)  //  <== Doesnt Work either
    yazdir()
}
}

below child class:

class child:Mother{
    var someVar:Int=1
    init(myVar:Int) {
        super.init()
        someVar = myVar
    }

}

I want to use "child" class in "Mother" class. But i got " not initialized at super.init call" error. Other view controller calls "Mother" class with "print" method such as:

@IBAction func buttonTest(sender : AnyObject) {
   var mom=Mother()
   mom.yazdir() 

}

The question is How i can use "child" class in "Mother" class? Thank you

Upvotes: 0

Views: 172

Answers (1)

holex
holex

Reputation: 24031

this line of code is not correct syntactically as is:

var subClass : child(mylevel:5) 

you need to define the type after the : (before the = if there is any) or you can use it without explicit type, like:

var subClass = child(mylevel:5)

Upvotes: 1

Related Questions