Initialize class-instance and access variables in Swift

后端 未结 2 1531
借酒劲吻你
借酒劲吻你 2020-12-07 03:50

I just created a class and gave it some vars. Unfortunately I can\'t access these variables in my iOS-Projects. However, the same syntax works in playground...<

相关标签:
2条回答
  • 2020-12-07 04:02

    The last two lines need to be inside a function. You can have only declarations in class scope, not expressions to be executed.

    0 讨论(0)
  • 2020-12-07 04:26

    The syntax for a class declaration is:

    class <class name>: <superclass>, <adopted protocols> {
      <declarations>
    }
    

    Your declaration of ViewController includes rex.age = 2 which itself is not a declaration, it is a statement - specifically a binary expression with an infix operator.

    0 讨论(0)
提交回复
热议问题