Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

files in Sources folder cannot access the other files' public classes or functions in Xcode playground

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

like image 379
jaspeRRR Avatar asked Nov 06 '22 02:11

jaspeRRR


1 Answers

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.

like image 113
matt Avatar answered Nov 13 '22 20:11

matt