swift - 在 Swift 中打印类变量 - 预期声明错误

标签 swift subclass

你知道出了什么问题吗? 我只想打印一个类的变量。 我认为打印必须在方法内部,但我不知 Prop 体如何。

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.h 中的声明。

...
self.name = name
...

...
self.level = level
...

希望有帮助。

代码中还缺少一个大括号。

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

关于swift - 在 Swift 中打印类变量 - 预期声明错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33243509/

相关文章:

ios - 如果当前 View Controller 被呈现并被推送,如何展开到正确的 View Controller ?

ios - 根据用户角色重用 UIViewController 子类的 Storyboard UI

excel - VBA 子类引用

iOS:隐藏 UIStackView subview

swift - 无法将用户 ID 指定为引用

ruby-on-rails - Rails 2 和 3 之间子类功能的差异

ios - 子类化 UIView 并添加属性

ios - 创建子类 C4Shapes(C4 框架)时获取未声明的标识符

swift - 从 IBAction 函数继续/推送到不同的 View Controller ?

ios - 如何在 UIScrollView 的末尾添加一个按钮以加载更多?