如何在Objective-C项目中导入和使用Swift Pod框架 [英] How to import and use Swift Pod Framework in Objective-C Project

查看:126
本文介绍了如何在Objective-C项目中导入和使用Swift Pod框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试检出CocoaPods新框架设置以使Pod正常运行,而在我的Objective-C项目中使用Swift时遇到了麻烦.

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预发行版0.35,您可以阅读有关如何使用和安装它的信息

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

这是我当前的Podfile:

Here's my current Podfile:

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

platform :ios, '8.0'

pod 'MBProgressHUD'
pod 'SLPagingViewSwift'

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

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.m In my project:

#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

这是SLPagingViewSwift声明:

class SLPagingViewSwift: UIViewController, UIScrollViewDelegate {

如您所见,

它是从UIViewController继承的,因此仅分配它并对其进行初始化就不成问题.如果我将文件单独添加为文件,则上面的代码运行良好.我知道它有效.

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.

tl; dr

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

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

故障排除

大多数情况下,我一直在尝试各种进口产品. Apple建议使用@import样式

Mostly I've been trying various imports. Apple recommends the @import style 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"

我还不时尝试其他一些Swift库,以查看是否可以单击任何东西.

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

在Cocoapods问题上,我看不到有什么可以解决的,我也未在他们的博客/发布资料中找到任何东西.

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

注意

如果我以老式方式将SLPagingViewSwift.swift文件单独添加到项目中,则效果很好.

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

推荐答案

我认为您必须将swift类声明为公共类,否则它将被视为内部类,并且只能在同一模块中看到,并且可能是为什么将其添加到与文件相同的项目中的原因,但作为框架却没有.我想到的另一件事是,框架可能需要在类声明的前面添加@objc,以便可以在Objective-C类中看到它.另请阅读 Mix and Match 表示在导入外部框架时,需要确保要导入的框架的定义模块"构建设置设置为是".您是否选择了其中任何一个选项?

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 Match between 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?

这篇关于如何在Objective-C项目中导入和使用Swift Pod框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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