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.
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: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.Then the view controller can render a BarChart with some code like this:
The screenshot of the rendered bar chart:
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: