0

I am creating a playground in Xcode and I do not understand why the "Sources" files cannot talk to each other. I am using SpriteKit which means it is ideal to have one swift file per scene, in my case I have one scene per level. I cannot use a quick workaround and add everything into one massive file... there has to be a better way. Both classes for the two swift files are public. You should be able to access them. Thanks!

I get this error when I try to an object from one class, LevelScene, in the other class TitleScene.

After this

let levelScene = LevelScene(fileNamed: "LevelScene")

This happens

Cannot find 'LevelScene' in scope

I am aware of this post. This solution still does not work for me.

Xcode playgrounds can't access swift files in Sources folder

I am on Xcode 12.4.

Thanks!

Joe
  • 69
  • 1
  • 12
  • 2
    This seems like a misunderstanding of playgrounds. There is just one playground. The Sources files are libraries available to the playground — not to each other. – matt Apr 01 '21 at 00:33
  • I have put everything in one source file. I still do not understand this logic given the answer from the question I linked up there. I could have sworn I had access control before this version came out. (Xcode 12.4). Thanks for the reply! – Joe Apr 01 '21 at 01:41

1 Answers1

0

You need to declare all your classes/struct in each file as public. So, if you have defined a struct like this in a file:

struct Node {
  var id: Int
}

You need to declare is like so:

public struct Node {
  var id: Int
}

Then your code should work. You can use a struct from one file in another file as long as you declare them as public. Similar concept goes for any functions/methods inside those structs/classes. Even with this solution, you may find Xcode 12.4 complaining about not finding the type in scope. That, I believe, is a known bug. I have the same bug on my Xcode 12.4. I am able to build and run the code even with that error.

mdzahedhossain
  • 125
  • 1
  • 14