3

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...

class ViewController: UIViewController 
{
    class Dog 
    {
        var age = 0
        var hungry = TRUE
    }
    var rex = Dog()
    rex.age = 2   //ERROR: EXPECTED DECLARATION
}

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How should the code look like to make it work properly?
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.