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

I am trying to check out the new CocoaPods platform setup to get some Pods, and I am having problems using Swift in my Objective-C project.

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

Here is my current subfile:

source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' pod 'MBProgressHUD' pod 'SLPagingViewSwift' 

MBProgressHUD is a general indicator of rotation, and SLPagingViewSwift is a random project that I found by typing Swift in search for cocoapods. Here is 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 

Here's the SLPagingViewSwift :

 class SLPagingViewSwift: UIViewController, UIScrollViewDelegate { 

As you can see, it inherits from the UIViewController , so you should not just select it and initialize it. If I add the file separately as soon as the file, the above code will work fine. I know this works.

tl; dr

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

Troubleshooting

Mostly I tried to use various imported goods. Apple recommends @import style here

enter image description here

But I tried several other varieties:

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

I also tried several other Swift libraries from time to time to find out if I can click anything.

I see nothing in Cocoapods problems that can help this, I also did not find anything in their blog / release.

Note

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

+66
objective-c swift cocoapods
Jan 17 '15 at 2:50
source share
1 answer

I think you should declare the swift class public, otherwise it is considered as an inner class and can only be shown in one module, and this may be the reason for adding it to the same project as the files, but not as a basis. Another thing that arises for me is that the infrastructure may need to add @objc before declaring the class so that it can be seen in objective-c classes. Also, after reading the Apple manual on Mix and Match between c and swift objects, he says that when importing an external framework, you need to make sure that the "Configure Modules" setting for the frame youre importing is set to Yes. Have you checked any of these options?

+41
Jan 20 '15 at 5:19
source share



All Articles