Joe
Joe

Reputation: 181

Xcode Playgrounds: Source files are unable to be accessed by each other

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!

Upvotes: 0

Views: 893

Answers (1)

mdzahedhossain
mdzahedhossain

Reputation: 125

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.

Upvotes: 0

Related Questions