在 XCode 个 Playgrounds 中相互引用的来源
Sources referencing each other in XCode Playgrounds
我的 Xcode Playground 的 Sources/
目录中有代码。我可以在我的操场上很好地引用它们,但我如何让它们相互引用?例如,假设我在 Sources/
目录中有 A.swift
和 B.swift
文件。我想让 A.swift
从 B.swift
访问某些内容,但随后我收到一条错误消息,指出 B.swift
的内容在 A.swift
.
中未定义
我怎样才能做到这一点?
将 public
属性添加到您想要在其他 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
更新:
我找到了 this thread,虽然我认为它不是专门针对这个主题的,但我相信它回答了这个问题,以下是公认的答案:
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.
所以,正如我之前所说,可以访问 sources 文件夹,但不能访问其他 playgrounds。
我的 Xcode Playground 的 Sources/
目录中有代码。我可以在我的操场上很好地引用它们,但我如何让它们相互引用?例如,假设我在 Sources/
目录中有 A.swift
和 B.swift
文件。我想让 A.swift
从 B.swift
访问某些内容,但随后我收到一条错误消息,指出 B.swift
的内容在 A.swift
.
我怎样才能做到这一点?
将 public
属性添加到您想要在其他 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
更新:
我找到了 this thread,虽然我认为它不是专门针对这个主题的,但我相信它回答了这个问题,以下是公认的答案:
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.
所以,正如我之前所说,可以访问 sources 文件夹,但不能访问其他 playgrounds。