I have an existing project which is written in Objective-C. I want to use the iOS Charts library by Daniel Gindi, which is in Swift, in that project. How can I achieve this?
importing swift framework into a objective-c project is related, but in my case I am using the library by installing it through Cocoapods as instructed here.
In that question, he is at least able to import the header, but in my case I am not able to.
step 1: Create Objective-C single view project.
step 2: Create a Podfile and add following lines -
use_frameworks!
pod 'Charts'
step 3: Install the pod
step 4: Try to import Charts in ViewController.h by @import Charts
but it gives an error at this point. The error is - Module Charts not found
Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .
You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.
Support Objective-C pods in a Swift project First, create your Podfile and add the pods you need as usual. Install them using the pod install command and open the . xcworkspace file created in your project folder. Pods should now be included in your workspace.
${Your project name}-Swift. h is for objc based project to use swift code I remember. It is generated by XCode, to translate swift code to a header file for objc to import. If you don't need to use swift in objc project, you don't need it.
In my existing project, I did #include Charts-Swift.h
in my project's main.m
file and this solved problem from me. @import Charts
worked perfectly now. This was done according to the instructions that are written in Apple documentation here.
I made a new project and tried Anni S's answer, this time I built the project with cmd+B
before writing any import statement and it worked too. I guess building it once after installing pod and before importing any module is necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With