简体   繁体   English

Swift在类中声明类

[英]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. 但是我收到“未在super.init调用中初始化”错误。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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