在 XCode Playgrounds 中相互引用的源 [英] Sources referencing each other in XCode Playgrounds

查看:30
本文介绍了在 XCode Playgrounds 中相互引用的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Xcode Playground 的 Sources/ 目录中有代码.我可以在我的操场上很好地引用它们,但是如何让它们相互引用?例如,假设我在 Sources/ 目录中有 A.swiftB.swift 文件.我想让 A.swiftB.swift 访问一些东西,但是我收到一个错误,说来自 B.swift 的东西是A.swift 中未定义.

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?

推荐答案

public 属性添加到要在其他 Playground 中访问的类、方法和属性.这应该可以解决您的问题:

Add the public attribute to the classes, methods, and properties that you want to access in other playgrounds. This should solve your problem:

您必须为源文件夹中的类、方法和属性添加公共访问属性,以使它们可以从主 Playground 文件访问,因为它们被编译器视为单独的模块

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.

从 Xcode 6.3 开始,Playgrounds 现在可以包含支持代码.他们仍然看不到同一个项目中的其他代码,但是可以将代码添加到 Playground 的支持文件夹中,该文件夹可以在 Playground 中使用.有关详细信息,请参阅 Swift 博客.

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.

所以,正如我之前所说的,sources 文件夹可以访问,但在其他 Playground 中不能访问.

So, as I said before, thing is the sources folder can be accessed, but not thing in other playgrounds.

这篇关于在 XCode Playgrounds 中相互引用的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆