5

For the life of me I cannot find the solution anywhere, so it seems to me it is a Xcode playground bug.

Within the Sources folder, despite me declaring a class as public, when I try to access the class from a separate file (within the Sources folder), compiler will give me an error message that says it cannot find the class in scope.

However, when I run the playground and ignore the message, the program will build and run successfully, and the error message will disappear until I start to modify the code.

Strangely enough, it doesn't happen at in an App project; it only happens in playground.

I've made a simple demonstration using a playground file called "test" and screenshoted what has happened, which you can see in the attached photo below, please advise! Thanks a lot!

Tried solutions include:

  1. restart Xcode
  2. restart Mac
  3. delete Xcode and reinstall Xcode

Cannot find Person class in scope despite it being marked public

cannot find Person class in scope despite it being marked public

But the Person class is marked as public

the Person class is marked as public

Code compiles successfully regardless

code compiles successfully regardless

Error disappear after running the playground, but comes right back as soon as I modify it

error disappear after running the playground, but comes right back as soon as I modify it

aheze
  • 24,434
  • 8
  • 68
  • 125
jaspeRRR
  • 51
  • 3

1 Answers1

1

Strangely enough, it doesn't happen at in an App project; it only happens in playground.

Actually that part is exactly what is not strange. App projects are predictable. Playgrounds are the work of the devil and it's easy to make them behave incorrectly.

What you're seeing is certainly a bug, especially because it can't readily be reproduced by others. On my machine, there are no error messages and everything runs fine:

enter image description here

enter image description here

enter image description here

You might try adding import test_Sources to the troublesome file, but I can't guarantee it will make a difference. What I really recommend is that you avoid playgrounds.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for the prompt reply! I'm not sure what the syntax would be for importing as a testable module, would that be "@testable" ? And noted, about playground being a buggy mess... what would be a substitute for playground? For most of the swift books I'm reading require me to use playground, thanks! – jaspeRRR Apr 16 '21 at 06:03