在 Swift 中打印 Class 变量 - 预期的声明错误

Print Class Variables in Swift - Expected Declaration Error

你知道这是怎么回事吗? 我只想打印一个 class' 变量。 我认为打印必须在方法内部,但我不知道具体如何。

import UIKit


class Class {
    var name: String
    init(name:String){
        self.name
}

class Subclass : Class {
    var level: Int
    init(name:String, level:Int){
        self.level
        super.init(name: name)
    }
}

var subclass1 = Subclass(name: "Hallo", level: 10)
print(subclass1.level)            // <-- here I get the error "Expected Declaration"

Screenshot of code

我对 Swift 不是很熟悉,但看起来您缺少 init.d 文件中的声明。

...
self.name = name
...

...
self.level = level
...

希望对您有所帮助。

您还缺少代码中的大括号。

class Class {
    ...
    init(...) {
    ...
->}//missing
}

非常感谢您的快速回复!

我的 init 中缺少的声明是创建最小工作示例时的粗心错误。问题的解决方案是缺少括号。

对不起我的轻率:(