Sources referencing each other in XCode Playgrounds

≯℡__Kan透↙ 提交于 2021-01-28 06:58:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!