85

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.

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

Here's my current 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.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 declaration:

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.

tl;dr

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

TroubleShooting

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

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.

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

Note

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

Community
  • 1
  • 1
Logan
  • 52,262
  • 20
  • 99
  • 128
  • From what I understand, you can always go from Objc to Swift, but not always the other way around since Swift has language features that simply don't exist in Objc... –  Jan 17 '15 at 02:52
  • @EdgarAroutiounian - You are correct in some respects, but with this type of class, it shouldn't be a problem because it inherits from an ObjC class. Also if I add just the file, my above snippet builds and runs fine. (see update) – Logan Jan 17 '15 at 02:54
  • Hey your question actually helped me a lot, I was trying to add FacebookSDK with cocoa pods, and got errors when trying to compile, "import FacebookSDK" did the trick – Michal Gumny Feb 17 '15 at 11:10
  • This question is actually very helpful: using `@import` in Objective-C -- which many of us have seen never or rarely, is new, helpful and necessary. Thanks for figuring this out. – Dan Rosenstark Feb 04 '16 at 04:58

3 Answers3

49

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?

Carlos Compean
  • 674
  • 6
  • 6
  • Public did it! Don't know why that had to be that way, but it did. It was my understanding that public was the default, glad to know I'm wrong. I guess that wins you the bounty (in 3 hrs), congrats :) – Logan Jan 20 '15 at 14:29
  • 3
    Ok, I know this a over a year late, but I feel obligated to answer this (because nobody else did). Swift defaults to internal (only accessible by the assigned group) for many reasons. – Erik Bean Jul 05 '16 at 03:31
  • 6
    If using a pod using swift framework inside a Objective C project, you need to just add #import "-Swift.h". In my case I needed to use Alamofire in my objective C project, so I used #import "Alamofire-Swift.h" to make it work.. – Swaroop S Feb 28 '18 at 05:38
  • 2
    in my case - the 'pod' is a swift-based framework, and its 'public' classes just don't get exported, and don't appear in the generated header. What now? – Motti Shneor Aug 17 '20 at 15:18
13

Jus use the

@import SwiftModuleName;

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

Jordi Puigdellívol
  • 1,710
  • 3
  • 23
  • 31
-2

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

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

#import "PODNAME-Swift.h"

and use classes from 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.

Szuwar_Jr
  • 683
  • 8
  • 11