如何在 Objective-C 项目中导入和使用 Swift Pod Framework

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

How to import and use Swift Pod Framework in Objective-C Project

objective-cswiftcocoapods

提问by Logan

I've been trying to checkout CocoaPods new framework setup to get some Pods going and I'm having trouble using the Swift one's in my Objective-C project.

我一直在尝试检查 CocoaPods 新框架设置以使一些 Pod 运行,但在我的 Objective-C 项目中使用 Swift 时遇到问题。

First things first, this is CocoaPods prerelease 0.35, you can read about how to use and install it here.

首先,这是 CocoaPods 预发布 0.35,您可以在此处阅读有关如何使用和安装它的信息

Here's my current Podfile:

这是我当前的 Podfile:

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '8.0'

pod 'MBProgressHUD'
pod 'SLPagingViewSwift'

MBProgressHUD is a common spinning indicator, and SLPagingViewSwift is a random project I found by typing Swift into the cocoapods search. Here's the ViewController.mIn my project:

MBProgressHUD 是一个常见的旋转指示器,SLPagingViewSwift 是我通过在 cocoapods 搜索中键入 Swift 找到的一个随机项目。这是ViewController.m在我的项目中:

#import "ViewController.h"

@import SLPagingViewSwift;
@import MBProgressHUD;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Works just fine
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    [hud show:YES];

    // Causes Error -- Won't build
    SLPagingViewSwift *sl = [[SLPagingViewSwift alloc] init];
}

@end

Here's the SLPagingViewSwiftdeclaration:

这是SLPagingViewSwift声明:

class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {

As you can see, it inherits from UIViewController, so it shouldn't be a problem to just allocate it and initialize it. If I add the file separately as just a file, the above code runs just fine. I know it works.

如您所见,它继承自UIViewController,因此只需分配它并初始化它应该不是问题。如果我将文件单独添加为一个文件,则上面的代码运行得很好。我知道它有效。

tl;dr

tl;博士

How can I use a pure Swift Framework created by CocoaPods in a pure Objective-C class?

如何在纯 Objective-C 类中使用由 CocoaPods 创建的纯 Swift 框架?

TroubleShooting

故障排除

Mostly I've been trying various imports. Apple recommends the @importstyle here

大多数情况下,我一直在尝试各种进口。苹果在这里推荐的@import款式

enter image description here

在此处输入图片说明

But I have been trying multiple other varieties:

但我一直在尝试多种其他品种:

// Compiler Error
#import <SLPagingViewSwift/SLPagingViewSwift.h>

// Builds Fine -- Doesn't Work
#import <SLPagingViewSwift/SLPagingViewSwift-Swift.h>
#import "SLPagingViewSwift-Swift.h"

I've also been trying a few other Swift libraries from time to time to see if I could make anything click.

我还时不时地尝试其他一些 Swift 库,看看我是否可以点击任何东西。

I don't see anything on the Cocoapods issues that can help this, I also didn't find anything in their blog / release stuff.

我在 Cocoapods 问题上没有看到任何可以帮助解决此问题的内容,我也没有在他们的博客/发布内容中找到任何内容。

Note

笔记

If I add the SLPagingViewSwift.swiftfile separately to the project the old fashioned way, it works just fine.

如果我SLPagingViewSwift.swift以老式的方式将文件单独添加到项目中,它就可以正常工作。

采纳答案by Carlos Compean

I think you have to declare the swift class as public, otherwise it is treated as an internal class and can be only be seen within the same module, and this could be the reason why adding it to the same project as files work, but as a framework doesn't. Other thing that occurs to me is that the framework may need to add @objc in front of the class declaration so that it can be seen within objective-c classes. Also reading Apple's guide of Mix and Matchbetween objective c and swift it says that when you import an external framework, you need to make sure the Defines Module build setting for the framework you're importing is set to Yes. Have you checked with any of those options?

我认为您必须将 swift 类声明为 public,否则它会被视为内部类并且只能在同一个模块中看到,这可能是将它添加到文件工作的同一个项目的原因,但作为框架没有。我想到的另一件事是框架可能需要在类声明前添加@objc,以便它可以在objective-c 类中看到。还阅读了Apple's guide of Mix and Matchbetween objective c and swift 它说当你导入一个外部框架时,你需要确保你正在导入的框架的定义模块构建设置设置为是。您是否检查过这些选项中的任何一个?

回答by Jordi Puigdellívol

Jus use the

强制使用

@import SwiftModuleName;

@import SwiftModuleName;

Syntax, and make sure the functions you want to use are public (and @objc)

语法,并确保您要使用的函数是公共的(和@objc)

回答by Szuwar_Jr

In my case there was no “use_frameworks!” into podfile (old project).

就我而言,没有“use_frameworks!” 进入 podfile(旧项目)。

I added it and then I was able to use import like that

我添加了它,然后我就可以像这样使用 import

#import "PODNAME-Swift.h"

#import "PODNAME-Swift.h"

and use classes from pod.

并使用 pod 中的类。

But finally I wasn't able to use that swift pod, because of lack objective c exposition. I believe this will be the issue in many cases.

但最终我无法使用那个 swift pod,因为缺乏客观的 c 说明。我相信这将是许多情况下的问题。