简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM