xcode 无法通过框架访问公共 swift 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37138815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:48:12  来源:igfitidea点击:

Not able to access public swift class through framework

iosobjective-cxcodeswiftxcode7

提问by Shakti

Project structure

Project structure

I created a project and imported it inside this framework. When i load this framework into a demo app, i am able to access the public class MyTestinside MyTest.swiftfile but not able to access the public class inside MessagesViewController.swift. Am i missing something? Can't we access files inside a project through a framework this way?

我创建了一个项目并将其导入到这个框架中。当我将此框架加载到演示应用程序中时,我可以访问文件MyTest内的公共类,MyTest.swift但无法访问MessagesViewController.swift. 我错过了什么吗?我们不能通过这种方式通过框架访问项目内部的文件吗?

采纳答案by Pete Hornsby

Restructure your project space so that you do not have your application inside your framework, then tackle linking files between the app and the framework.

重构您的项目空间,使您的应用程序不在您的框架中,然后处理应用程序和框架之间的链接文件。

  1. Create a xcworkspace file to contain all of your sub-projects. For more information check out the Xcode Workspacesection in the Xcode Conceptsdocument
  1. 创建一个 xcworkspace 文件以包含您的所有子项目。有关更多信息,请查看Xcode 概念文档中的Xcode 工作区部分

enter image description here

enter image description here

  1. Open the workspace file and add your framework and app projects by dragging the xcodeproj files to the "Project Navigation" panel. Be careful when adding the files as Xcode as a tendency to place one project inside another, you don't want this. enter image description here

  2. Build your framework and then link it to the the app project. This will also make all you framework files available to your app project as long as they are public(see below for more details). Navigate to the FireChat-Swift app project target in the editor view then from the frameworks project open the product directory and drag the framework file to the Embedded Binaries section of the general tab.

  1. 打开工作区文件并通过将 xcodeproj 文件拖到“项目导航”面板来添加您的框架和应用程序项目。添加文件作为 Xcode 时要小心,因为倾向于将一个项目放在另一个项目中,您不希望这样。 enter image description here

  2. 构建您的框架,然后将其链接到应用程序项目。这也将使您的所有框架文件可用于您的应用程序项目,只要它们是公开的(有关更多详细信息,请参见下文)。在编辑器视图中导航到 FireChat-Swift 应用程序项目目标,然后从框架项目打开产品目录并将框架文件拖到常规选项卡的嵌入式二进制文件部分。

enter image description here

enter image description here

  1. Now your framework is linked with your app project. You will need to import your framework in your app's source code. enter image description here

  2. Add new files to your framework and remember they need to be public should you require them to be used outside of the framework - i.e. by your app.

  1. 现在您的框架已与您的应用程序项目相关联。您需要在应用程序的源代码中导入您的框架。 enter image description here

  2. 将新文件添加到您的框架并记住,如果您需要在框架之外使用它们(即由您的应用程序使用),它们需要是公开的。

回答by Luca Angeletti

Did you add MessagesViewController.swiftto the Target inside the original Project?

您是否添加MessagesViewController.swift到原始项目中的目标?

To check it just open the original project, select MessagesViewController.swiftand take a look at the right sidebar (Target Membership section).

要检查它,只需打开原始项目,选择MessagesViewController.swift并查看右侧边栏(目标成员资格部分)。

回答by George_E

I had this problem, and I fixed mine by going to my framework and adding target membership to Testsand UITests:

我遇到了这个问题,我通过转到我的框架并将目标成员资格添加到Tests和来解决我的问题UITests

enter image description here

enter image description here

Strange fix, but I guess it works! ?♂?

奇怪的修复,但我想它有效!?♂?

回答by Ash

This can also occur as part of a bug in Xcode, though I'm not sure what causes it - possibly dragging files from another project into your library. I just fixed this by closing down and restarting Xcode, then cleaning the build folder (hold down Altthen select the option from the Productmenu).

这也可能作为 Xcode 中错误的一部分发生,但我不确定是什么原因造成的 - 可能是将文件从另一个项目拖到您的库中。我只是通过关闭并重新启动 Xcode 来解决这个问题,然后清理构建文件夹(按住Alt然后从Product菜单中选择该选项)。