ios 在 Swift 项目、Xcode 中导入框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24057756/
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-08-31 00:31:03  来源:igfitidea点击:

Import Framework in Swift Project, Xcode

iosxcodeswiftframeworks

提问by phnmnn

I'm trying to import myFrameworkinto a project. I've added myFrameworkin Build Phases->Link Binary With Libraries.

我正在尝试导入myFramework到一个项目中。我myFramework在构建阶段-> 链接二进制文件中添加了库。

Objective-c works:

Objective-c 的工作原理:

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

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

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

import UIKit
import myFramework

According to the Swift documentation:

根据Swift 文档

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;

导入外部框架

您可以导入具有纯 Objective-C 代码库、纯 Swift 代码库或混合语言代码库的外部框架。无论框架是用一种语言编写的还是包含两种语言的文件,导入外部框架的过程都是相同的。当您导入外部框架时,请确保您正在导入的框架的定义模块构建设置设置为是。

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

迅速

import FrameworkName

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

目标-C

@import FrameworkName;

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

myFramework使用 Xcode 5创建。Xcode 5 没有“定义模块”构建设置。

Where is the problem?

问题出在哪儿?

采纳答案by mic

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.

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

The part of the documentation you're referring to is about frameworks within differenttargets. 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.

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

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.

但是,您的框架是用 Objective-C 编写的。所以,你必须做的是导入斯威夫特面临班在桥接头所描述这里

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.

请注意,这与模块的 Swift 导入无关。桥接头文件中的 import 指令只是通知编译器将 Objective-C 头文件“翻译”为 Swift 语法,并使公共头文件对 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.
  • 首先在 bridging-header 中导入您感兴趣的头文件。您只需要导入您将在 Swift 中与之交互的标头。

  • 尝试在此阶段编译您的项目。如果 XCode 找不到框架的头文件,您的问题可能与 Swift 或 XCode 6 无关,而是与包含框架的一般问题有关。

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

Hope this helps.

希望这可以帮助。

回答by A.G

On Swift:

在斯威夫特上:

Create Framework:-

创建框架:-

  1. Start Xcode -> Create a new Xcode Project -> iOS -> Framework & Library -> Cocoa Touch Framework -> Name the framework(ex. sampleCocoaFramework) -> Create.

  2. Set Target -> General -> Deployment info -> Deployment Target.

  3. Add a public class: File -> New File -> iOS -> Swift File -> Name it (ex. openCocoaClass) -> Create.

  4. Now add some code to the openCocoaClass.swift.

    import Foundation
    
    public class openCocoaClass {
    
        public init() {
    
        }
    
        public var samplePublicVariable = "samplePublicVariable @ openCocoaClass"
    
        public func samplePublicFunction()
        {
            print("samplePublicFunction @ openCocoaClass")
        }          
    

    }

  5. Clean the project : Product -> Clean

  6. Configure the scheme settings : Product -> Scheme -> Edit Scheme -> Run -> Build Configuration -> Release.

  7. Build the framework : Product -> Build.

  8. Export the framework : Products -> Select the framework -> Identity and type -> Full Path -> Released Framework.
  1. 启动 Xcode -> Create a new Xcode Project -> iOS -> Framework & Library -> Cocoa Touch Framework -> Name the framework(ex. sampleCocoaFramework) -> Create。

  2. 设置目标 -> 常规 -> 部署信息 -> 部署目标。

  3. 添加一个公共类:File -> New File -> iOS -> Swift File -> Name it (ex. openCocoaClass) -> Create。

  4. 现在向 openCocoaClass.swift 添加一些代码。

    import Foundation
    
    public class openCocoaClass {
    
        public init() {
    
        }
    
        public var samplePublicVariable = "samplePublicVariable @ openCocoaClass"
    
        public func samplePublicFunction()
        {
            print("samplePublicFunction @ openCocoaClass")
        }          
    

    }

  5. 清理项目:产品 -> 清理

  6. 配置方案设置:产品->方案->编辑方案->运行->构建配置->发布。

  7. 构建框架:产品 -> 构建。

  8. 导出框架:产品 -> 选择框架 -> 身份和类型 -> 完整路径 -> 已发布框架。

Adding Framework to Project:-

向项目添加框架:-

  1. Start a Xcode project and name it (ex. CocoaFrameworkTest).

  2. Drag and drop the sampleCocoaFramework.framework to the CocoaFrameworkTest's project folder.

  3. Target -> General -> Embed Binaries -> Add Other -> Select Framework -> Copy Items if needed -> Done.

  1. 启动一个 Xcode 项目并为其命名(例如 CocoaFrameworkTest)。

  2. 将 sampleCocoaFramework.framework 拖放到 CocoaFrameworkTest 的项目文件夹中。

  3. Target -> General -> Embed Binaries -> Add Other -> Select Framework -> Copy Items if needed -> Done。

Accessing Framework on ViewController:-

在 ViewController 上访问框架:-

import UIKit
import sampleCocoaFramework


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let frameworkObject =  openCocoaClass.init()
        frameworkObject.samplePublicFunction()
        print(frameworkObject.samplePublicVariable)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

回答by Tomer Even

According to the Swift documentation

根据Swift 文档

To import Objective-C code into Swift from the same target

  1. In your Objective-C bridging header file, import every Objective-C header you want to expose to Swift. For example:

    #import "XYZCustomCell.h"

    #import "XYZCustomView.h"

    #import "XYZCustomViewController.h"

  2. Under Build Settings, make sure the Objective-C Bridging Header build setting under Swift Compiler - Code Generation has a path to the header. The path must be directly to the file itself, not the directory that it's in. The path should be relative to your project, similar to the way your Info.plist path is specified in Build Settings. In most cases, you should not need to modify this setting.

Any public Objective-C headers listed in this bridging header file will be visible to Swift. The Objective-C functionality will be available in any Swift file within that target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

let myCell = XYZCustomCell()
myCell.subtitle = "A custom cell"

将 Objective-C 代码从同一目标导入 Swift

  1. 在你的 Objective-C 桥接头文件中,导入你想要暴露给 Swift 的每个 Objective-C 头文件。例如:

    #import "XYZCustomCell.h"

    #import "XYZCustomView.h"

    #import "XYZCustomViewController.h"

  2. 在 Build Settings 下,确保 Swift Compiler - Code Generation 下的 Objective-C Bridging Header 构建设置具有指向标头的路径。路径必须直接指向文件本身,而不是它所在的目录。路径应该相对于您的项目,类似于在构建设置中指定 Info.plist 路径的方式。在大多数情况下,您不需要修改此设置。

此桥接头文件中列出的任何公共 Objective-C 头文件都将对 Swift 可见。Objective-C 功能将自动在该目标内的任何 Swift 文件中可用,无需任何导入语句。使用与系统类相同的 Swift 语法使用自定义的 Objective-C 代码。

let myCell = XYZCustomCell()
myCell.subtitle = "A custom cell"

Also, make sure the "Defines Module" build setting under "Packaging" is set to "Yes."

另外,请确保“Packaging”下的“Defines Module”构建设置设置为“Yes”。

回答by gustavo.a.hansen

you need in your objective c project a public header with the same name of you app, in you case FrameworkName.h and add all the classes that you want to expose (those classes should be added as public header in the project properties) Once you do that, you add the framework and add the reference to your public header import FrameworkName

您需要在您的目标 c 项目中使用与您的应用程序同名的公共头文件,在您的情况下为 FrameworkName.h 并添加您想要公开的所有类(这些类应作为公共头文件添加到项目属性中)一旦您这样做,您添加框架并添加对公共标头导入 FrameworkName 的引用

回答by V V

You can import external objC framework into swift project using below syntax:

您可以使用以下语法将外部 objC 框架导入 swift 项目:

#import "objCExternalFramework-name/headerfilename.h"

#import "objCExternalFramework-name/headerfilename.h"

回答by Sukhi

Okay, it's Xcode 7.2 now. And what I observed is, swift import is case sensitive. Example - import uikitwill not work. You'll have to type it as import UIKit.

好的,现在是 Xcode 7.2。我观察到的是,快速导入区分大小写。示例 - import uikit将不起作用。您必须将其键入为import UIKit.

just my two cents.

只有我的两分钱。

回答by Sazzad Hissain Khan

Latest XCode provides option to embed framework into other projects. This link (https://stackoverflow.com/a/37328591/1084174) worked well for me.

最新的 XCode 提供了将框架嵌入其他项目的选项。这个链接(https://stackoverflow.com/a/37328591/1084174)对我来说效果很好。

回答by Vasanth

Just command+Bwould help!

只是 command+B会有所帮助!

The why: Adding the framework through Cocoapodswill create another pod project with the required frameworks in it. For the main project to identify the frameworks, the static libraries of both the projects have to be bridged. Building the workspace is needed for that to happen.

原因:通过添加框架Cocoapods将创建另一个包含所需框架的 pod 项目。为了让主项目识别框架,必须桥接两个项目的静态库。要实现这一点,需要构建工作区。