1

I've been developing an objc project with Xcode 8.2 and iOS8~ support.

I installed a swift library (https://github.com/danielgindi/Charts) via Carthage, but if I call any method of this library, app crashes with "unrecognized selector sent to instance" error.

I can make it work via Cocoapods, but the build time will be very slow when I use Swift library with Cocoapods, so I want to use Carthage if possible.

Is there any way to use swift library in objc project via Carthage?

Basically, I installed Charts library via Carthage based on the page. https://github.com/Carthage/Carthage/blob/master/README.md

I only used embedded binaries to add the Charts.framework instead of Linked Frameworks since it solved the crash issue on the startup.

1 Answer 1

5

Is there any way to use swift library in objc project via Carthage?

Once you have added the Charts.framework to the Embedded Binaries in your project settings, you can import Swift code into ObjC as follows. For example, with a sample project named ChartsObjCSample, you need to:

import "ChartsObjCSample-Swift.h"

Now you should be able to use the Charts API in your ObjC code.

However, at times the above -Swift.h file is not created, in which case you can create a dummy Swift file, add the brining header as per Xcode instructions and then clean, build again, which should create the -Swift.h file.

Finally, to use the Charts API for example to create a BarChart, you can have a ViewController with the storyboard view class assigned as BarChartView as shown below.

enter image description here

Then the view controller can render a BarChart with some code like this:

#import "ViewController.h"
#import "ChartsObjCSample-Swift.h"
@import Charts;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    BarChartView* barChartView = (BarChartView*)self.view;

    NSMutableArray* dataEntries = [[NSMutableArray alloc] init];
    for(int i = 0; i < 100; i++) {
        BarChartDataEntry* dataEntry = [[BarChartDataEntry alloc] initWithX:5.0 * arc4random_uniform(12) y:100.0];
        [dataEntries addObject:dataEntry];
    }

    BarChartDataSet* dataSet = [[BarChartDataSet alloc] initWithValues:dataEntries label:@"Visitor Count"];

    BarChartData* barChartData = [[BarChartData alloc] initWithDataSet:dataSet];
    barChartView.data = barChartData;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

The screenshot of the rendered bar chart:

enter image description here


The above sample code is available on GitHub here.


From apple docs: To import Swift code into Objective-C from the same target

Import the Swift code from that target into any Objective-C .m file within that target using this syntax and substituting the appropriate name:

#import "ProductModuleName-Swift.h"
6
  • Hi, thanks for your reply. I wrote #import "Charts-Swift.h" in my barChartViewController.m file, but file not found error comes out. I also tried "Build Settings under Packaging, my Defines Module is set to Yes", but no luck. Is there anything left?
    – Umeumeume
    Commented Dec 26, 2016 at 7:14
  • When I type @import Charts; it doesn't show the error, but when I use some methods, it crashes.
    – Umeumeume
    Commented Dec 26, 2016 at 7:19
  • I think you may have to set Always Embed Swift Standard Libraries to YES. Ref: developer.apple.com/library/content/qa/qa1881/_index.html. Note in Xcode 8 onwards, the Embedded Content Contains Swift Code has been deprecated and renamed to Always Embed Swift Standard Libraries. Commented Dec 26, 2016 at 7:31
  • Hello, I tried Always Embed Swift Standard Libraries to YES, but still it shows the same error..
    – Umeumeume
    Commented Dec 26, 2016 at 8:00
  • @NonUmemoto I have updated the answer, and also added a link to a simple working project on GitHub. Hope this helps. Commented Dec 26, 2016 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.