Kev*_*vin 7 xcode swift swift-playground
注意:这与导入通用 swift 文件(可以使用 Sources 文件夹完成)是不同的问题。
我有一个有 2 页的操场,我想使用在第二页的第一页中定义的协议。我将使用 JSON 转换的示例。
JSON.xcplaygroundpage
import Foundation
protocol JSONConvertible {
func jsonValue() -> String
}
Run Code Online (Sandbox Code Playgroud)
JSONArray.xcplaygroundpage
import Foundation
//Undeclared type JSONConvertible
extension Array : JSONConvertible {
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下导入:
import MyPlayground
import MyPlayground.JSON
import JSON
import JSON.Contents (in finder the file name is actually Contents.swift)
Run Code Online (Sandbox Code Playgroud)
我还尝试将 JSON.xcplaygroundpage 添加到 Source 文件夹和JSONArrayResources 文件夹中。
注意:我意识到我可以将协议定义放在一个单独的地方,JSON.swift并将其包含在我的项目 Sources 文件夹中。这并不能真正回答我的问题。
这适用于 Xcode 8.3.3。
对于多个页面共有的代码,将其放在顶级Sources组下的单独文件中。确保在正确的位置有正确的 Swift 访问控制关键字。
来自http://help.apple.com/xcode/mac/8.2/#/devfa5bea3af 的注意事项:
...辅助 Swift 源文件必须使用public关键字导出它。这包括类、方法、函数、变量和协议。
public class DoIExist { public init(){ print("Woot!")} }
Run Code Online (Sandbox Code Playgroud)
然后您可以在所有其他页面中引用它。像这样:
//: [Previous](@previous)
let d = DoIExist()
//: [Next](@next)
Run Code Online (Sandbox Code Playgroud)
您可以看到它的工作原理是因为控制台输出(“Woot!”)和视图结果装订线。
为了实现这一点,我按照Apple Xcode 文档中关于 Playgrounds 的说明进行操作。当 Apple 不可避免地移动这些文档并且不提供转发链接时,请继续阅读我是如何找到它的。
我搜索了在cocoaprist的回答链接中找到的线索:“ios recipes Playground Help Add Auxilliary Code to a Playground”。这导致 Apple 当前在标题为“向操场添加辅助代码”的部分中标题为“使用操场”的章节中调用“Xcode 帮助”的文档。
coc*_*est -1
刚刚让它与苹果的指令一起工作:https://developer.apple.com/library/ios/recipes/Playground_Help/Chapters/AddAuxilliaryCodetoaPlayground.html
确保声明您的课程是公开的。另外,我必须重新启动 Xcode 才能生效。
| 归档时间: |
|
| 查看次数: |
2753 次 |
| 最近记录: |