问题
I have code in my Sources/
directory in my Xcode Playground. I can reference them fine in my playground, but how do I allow them to reference each other? For example, suppose I have A.swift
and B.swift
files in the Sources/
directory. I would like to have A.swift
access something from B.swift
, but then I get an error saying that things from B.swift
are undefined in A.swift
.
How can I accomplish this?
回答1:
Add the public
attribute to the classes, methods, and properties that you want to access in other playgrounds. This should solve your problem:
You have to add public access attribute to your classes, methods and properties in source folder to make them accessible from main playground file as they treated as separate module by compiler
Update:
I found this thread, which although is not on this topic specifically I believe answers the question, here is what the accepted answer says:
They cannot. Playgrounds are self-contained. This will hopefully change in the future.
Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.
So, as I said before, thing is the sources folder can be accessed, but not thing in other playgrounds.
来源:https://stackoverflow.com/questions/33880727/sources-referencing-each-other-in-xcode-playgrounds