Error creating class reference and concatenating methods in Swift

Asked

Viewed 77 times

0

Good night.

I have two classes and created an instance referencing the parent class calling its method and the same does not run and shows the following sentence:

"Swift Expressions are not allowed at the top level"

PS: I’ve tried everything, created, recreated... they’re all in the same group in Xcode

What can it be?

Follows print

inserir a descrição da imagem aqui

  • 1

    Voce has to place its code inside a function/method or inside viewDidLoad or viewDidAppear

1 answer

1


The problem is that you are putting code to run directly in the top-level file, just as you would in languages like Python and Javascript.

Swift, as well as C, C++, Java, among others, only accepts execution of code within functions. That is, lines 7 and 8 of your code must be within a function.

As you are programming for iOS, you can put in any function of the application lifecycle, such as viewDidLoad and viewDidAppear.

  • By the way, in the Xcode playground, it works normally. Thanks for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.