简体   繁体   English

将我自己的 Swift 类导入 Playground

[英]Import my own Swift class into Playground

I have seen several different posts on this subject, but none seem to solve what I think is a basic problem.我看过几个关于这个主题的不同帖子,但似乎没有一个能解决我认为的基本问题。 In my project I have the following:在我的项目中,我有以下内容:

Hi Class I have a Hi Class with a since method shown below嗨班我有一个嗨班,其方法如下所示

func sayHi(){
    println("hi")

}

Playground I have a playground where I try to import my Hi Class.操场我有一个操场,我在那里尝试导入我的 Hi Class。

The problem: My playground cannot see/import the Hi class.问题:我的操场无法看到/导入 Hi 类。 I know my Hi class is working as I can call it from a view controller without a problem.我知道我的 Hi 类正在工作,因为我可以从视图控制器中毫无问题地调用它。

Here the response from stack/apple forums that seem the most relevant这里似乎最相关的堆栈/苹果论坛的回应

It is now possible to import your own frameworks into a playground.现在可以将您自己的框架导入到 Playground 中。 This provides a way to share code between your applications and playgrounds, which can both import your frameworks.这提供了一种在您的应用程序和 Playgrounds 之间共享代码的方法,它们都可以导入您的框架。 To do this, your playground must be in the same workspace as the project that produces your framework.为此,您的 Playground 必须与生成您的框架的项目位于同一工作区中。 You must have already built your framework.您必须已经构建了您的框架。 If it is an iOS framework, it must be built for a 64-bit run destination (eg iPhone 5s).如果是 iOS 框架,则必须为 64 位运行目标(例如 iPhone 5s)构建。 You must have an active scheme which builds at least one target (that target's build location will be used in the framework search path for the playground).您必须有一个至少构建一个目标的活动方案(该目标的构建位置将用于 Playground 的框架搜索路径)。 Your "Build Location" preference (in advanced "Locations" settings) should not be set to "Legacy".您的“构建位置”首选项(在高级“位置”设置中)不应设置为“旧版”。 If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes".如果您的框架不是 Swift 框架,则“定义模块”构建设置必须设置为“是”。 Once all these conditions are fulfilled, importing your framework will work in a playground一旦满足所有这些条件,导入您的框架将在操场上工作

Source: How to I import 3rd party frameworks into Xcode Playground?来源: 如何将 3rd 方框架导入 Xcode Playground?

Can anyone point me to a step by step on how to do this ?谁能告诉我一步一步如何做到这一点?

In Xcode 7 there is a Sources folder in the Navigator Cmd 1 that will import any swift code locally in your playground.在 Xcode 7 中,Navigator Cmd 1中有一个 Sources 文件夹,它将在您的 Playground 中本地导入任何 swift 代码。

操场

Beware that you need to mark the classes and functions in the Sources as public.请注意,您需要将 Sources 中的类和函数标记为 public。

It's been over 3 years and I really hope you have been able to figure this out.已经过去 3 年多了,我真的希望你能够解决这个问题。 Anyway, here's an answer for anyone experiencing the same problem.无论如何,这是为遇到相同问题的任何人提供的答案。

Source code within the Sources folder are automatically imported. Sources文件夹中的源代码会自动导入。 Simply ensure your class and methods are marked as public .只需确保您的类和方法被标记为public And your class has public init method.你的班级有公共init方法。

See screenshot below.请参阅下面的屏幕截图。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM