Swift 项目、Xcode 中的导入框架 [英] Import Framework in Swift Project, Xcode

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

问题描述

我正在尝试将 myFramework 导入到项目中.我在 Build Phases->Link Binary With Libraries 中添加了 myFramework.

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

Objective-c 的工作原理:

Objective-c works:

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

但是在 Swift 中,我得到一个 No such module myFramework 错误:

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

import UIKit
import myFramework

根据 Swift 文档:

导入外部框架

您可以导入具有纯 Objective-C 的外部框架代码库、纯 Swift 代码库或混合语言代码库.这导入外部框架的过程是否相同框架是用一种语言编写的或包含来自这两种语言的文件语言.导入外部框架时,请确保为您要导入的框架定义模块构建设置是.

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.

你可以将一个框架导入到不同的 Swift 文件中使用以下语法定位目标:

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

SWIFT

import FrameworkName

您可以使用以下命令将框架导入到不同目标中的任何 Objective-C .m 文件中语法:

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

目标-C

@import FrameworkName;

我使用 Xcode 5 创建了 myFramework.Xcode 5 没有定义模块"构建设置.

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

问题出在哪里?

推荐答案

如果我理解正确,您的框架没有单独的构建目标(您已经使用 Xcode 5 构建了它)并将框架包含到您的项目的构建目标.

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.

在您的情况下,您无法在 Swift 文件中导入框架.这就是您收到错误消息 No such module myFramework" 的原因.myFramework 不是模块——它是您项目模块的一部分(默认情况下由您的产品名称确定).因此,您的框架中的类应该是可访问的.

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.

但是您的框架是用 Objective-C 编写的.因此,您要做的就是按照 这里.

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.

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

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.

那你现在应该怎么做?

  • 首先在bridging-header中导入你感兴趣的头文件.您只需要导入将在 Swift 中与之交互的标头.

  • 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.

尝试在这个阶段编译你的项目.如果 Xcode 找不到框架的头文件,则您的问题可能与 Swift 或 Xcode 6 无关,而是一般包含框架的问题.

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.

之后尝试实例化你在桥接头中导入的类,也许在你的 AppDelegate.swift 中.Xcode 自动补全应该为您提供类型名称.

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.

希望这会有所帮助.

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

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