在Swift Project,Xcode中导入框架 [英] Import Framework in Swift Project, Xcode

查看:109
本文介绍了在Swift Project,Xcode中导入框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 myFramework 导入到一个项目中。我已经在Build Phases-> Link Binary With Libraries中添加了 myFramework



Objective-c作品: p>

  #import< UIKit / UIKit.h> 
#import< myFramework / myFramework.h>

但是在Swift中,我得到了一个没有这样的模块myFramework error:

  import UIKit 
import myFramework

根据 Swift文档


导入外部框架



您可以导入具有纯粹的Objective-C
代码库的外部框架,纯粹的Swift代码库,或混合语言代码库。无论
框架是用单一语言编写还是包含两种
语言的文件,用于导入外部框架的
过程都是相同的。当您导入外部框架时,请确保您要导入的框架的
Defines Module构建设置被设置为
为Yes。



您可以使用以下语法将框架导入到不同
目标中的任何Swift文件中:

SWIFT

  import FrameworkName 

您可以使用以下
语法将框架导入到任何Objective-C .m文件中:

  @import FrameworkName; 


我创建了 myFramework 使用Xcode 5. Xcode 5没有Defines Module构建设置。



问题在哪里?
<如果我正确地得到你,你的框架没有单独的构建目标(你已经使用XCode 5构建它),并将框架包含到项目的构建中目标。



您参考的文档部分是关于 不同的 目标中的框架。
由于您的框架在项目目标中,因此这部分文档不适用于此。


在您的情况下,您无法在Swift文件中导入框架。这就是为什么你会收到错误消息没有这样的模块myFramework
myFramework不是模块 - 它是您项目模块的一部分(默认由您的产品名称决定)。因此,您的框架中的类应该是可访问的。



然而,您的框架是用Objective-C编写的。所以你必须做的是按照 here。



请注意,这与Swift导入模块无关。 bridging-header文件中的import指令只是通知编译器将Objective-C头文件翻译为Swift语法,并使公共头部对Swift可见。



那你现在该怎么做?


  • 首先在桥接头中导入您感兴趣的头文件。您只需要导入您将在Swift中交互的头文件。


  • 尝试在此阶段编译您的项目。如果XCode无法找到框架的头文件,那么您的问题可能与Swift或XCode 6无关,但通常包含框架的问题。


  • 之后尝试实例化您在桥接头中导入的类,也许在您的AppDelegate.swift中。 XCode自动完成应该为您提供类型名称。



希望这有助于您。


I'm trying to import myFramework into a project. I've added myFramework in Build Phases->Link Binary With Libraries.

Objective-c works:

#import <UIKit/UIKit.h>
#import <myFramework/myFramework.h>

But with in Swift, I get a No such module myFramework error:

import UIKit
import myFramework

According to the Swift documentation:

Importing External Frameworks

You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to Yes.

You can import a framework into any Swift file within a different target using the following syntax:

SWIFT

import FrameworkName

You can import a framework into any Objective-C .m file within a different target using the following syntax:

OBJECTIVE-C

@import FrameworkName;

I created myFramework using Xcode 5. Xcode 5 doesn't have a "Defines Module" build setting.

Where is the problem?

解决方案

If I get you correctly you don't have a separate build target for your framework (you already built it with XCode 5) and included the framework into your project's build target.

The part of the documentation you're referring to is about frameworks within different targets. Since your framework is in the project's target this part of the documentation doesn't apply here.

In your case you can't do an import of the framework in your Swift file. That's why you get the error message "No such module myFramework". myFramework is no module -- it is part of your project's module (which is by default determined by your product name). As such the classes in your framework should be accessible.

However your framework is written in Objective-C. So what you have to do is to import the Swift facing classes in your bridging-header as described here.

Please note that this has nothing to do with the Swift import of a module. The import directive in the bridging-header file just notifies the compiler to 'translate' Objective-C header files to Swift syntax and makes the public header visible to Swift.

So what should you do now?

  • First import the header files you're interested in in the bridging-header. You only need to import the headers you will interact with in Swift.

  • Try to compile your project in this stage. If XCode can't find the header files of the framework your problem is probably not related to Swift or XCode 6 but a problem with including frameworks in general.

  • After that try to instantiate a class you imported in the bridging-header, maybe in your AppDelegate.swift. XCode auto-completion should offer you the type names.

Hope this helps.

这篇关于在Swift Project,Xcode中导入框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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