I'm currently translating an Objective-C project I have over to Swift. In the process, I have a few implemented 3rd party libraries that are pure Objective-C like MFSideMenu and Parse.
I've created a Bridging-Header.h file per Apple's documentation and other's recommendations to bridge the Objective-C files with the Swift code. This works fine for frameworks like Parse. Unfortunately, when importing MFSideMenu and running the application, I receive the attached errors below.
I was contemplating if this was because a library needed to be manually imported into the Objective-C files using it, so I attempted importing UIKit and Foundation in the project and manually added the frameworks without any success. Any assistance would be greatly appreciated.
Turns out, the compiler for swift no longer supports non modular based imports for frameworks. You must change all references to frameworks to #import statements rather #import.
I had the same problem, add code below at the header file of this class
#import <UIKit/UIKit.h>
it can help you
Related
I am trying to integrate this https://github.com/gareth-cross/kalman-ios project with my own Swift project in Xcode.
When building the project I receive 2 errors:
kalman-ios/matrix.hpp:28:10: error: 'cmath' file not found
failed to emit precompiled header
'/Users/.../Build/Intermediates.noindex/PrecompiledHeaders/Tron-iOS-Bridging-Header-
swift_J9ENU1M0P5CE-clang_M49XZJR5TLTE.pch'
for bridging header '/Users/.../Filter/Tron-iOS-Bridging-Header.h'
My project is purely Swift, and the kalman-ios project is a mixture of ObjC and ObjC++.
My bridging header file is:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "KFEstimator.h"
For more context, the kalman-ios project is structured as:
KFEstimator.mm makes calls to AttitudeESKF.cpp makes calls to matrix.cpp
My initial thought was the cmath file error was the cause for the bridging header error. But I have almost no experience outside Swift and don't know how to go about fixing it. Up until yesterday, I wasn't even aware that you could use ObjCPP.
Any help is appreciated!
I have solved the issue. I was mistaken and believed that the repository was Objective-C making calls to Objective-C++.
It was actually all Objective-C++.
I was able to make direct calls to the Objective-C++ functions by following this video guide.
I know those objective-c files must be converted to swift manually (with some help from several online converting tools),
the question is...
how about frameworks that're added to the project?
And third parties written in objective-c added via cocoapods?
Do I need to remove those frameworks, re-add third parties of their swift version, or let Xcode warn me with any possible error?
Maybe just keep them and add some bridge file to cope with those?
Need some information before I mess everything up...
Any link or file guide would be appreciated!
Obj-c frameworks can all be used in swift and will automatically be bridged to swift so you can access them from swift code (create an obj-c bridge, google it). You can add your own .h files to this obj-c bridge file too and use your obj-c code in swift. There's no need to search for swift libraries or anything.
I have a mixed Swift and Objective-C project. To access a Swift public API in Objective-C, the API consumer currently has to import "MyModule-Swift.h". I already have a Objective-C umbrella header called "MyModule.h". But, importing "MyModule.h" is not going to work for the Swift APIs. I tried importing "MyModule-Swift.h" in the umbrella header, but it doesn't find it (I'm assuming since it is generated on the fly by Xcode).
Any suggestions so that an API consumer can always import "MyModule.h" to use public APIs written in Swift/Objective-C will be really appreciated.
====================================================================
Edit: I apologize for not taking the time to properly frame the question. I have a framework called MyModule.
I have a Objective-C class called ABUser,
#interface ABUser: NSObject
- (void)walk;
#end
I wanted to add new behavior to this class using Swift, so I defined an extension
extension ABUser {
func swiftWalk()
}
Now, say I wanted to access the swiftWalk method defined on ABUser from an Objective-C app, I would have to #import <MyModule/MyModule-Swift.h>. #import <MyModule/MyModule.h> would work if I wanted to use the walk method. This is assuming the umbrella header MyModule.h imports ABUser.h.
I always wanted the Objective-C app to #import <MyModule/MyModule.h> and never have to worry about whether an API was written in Objective-C or Swift in the framework. So, I tried importing MyModule-Swift.h in the umbrella header MyModule.h. But, my Objective-C app didn't compile if do that. Not sure, if this is because MyModule-Swift.h is generated by Xcode on the fly during the build process.
Edit 2: Added a sample project to reproduce this issue here: https://github.com/priteshshah1983/MyObjectiveCApp
The relevant code is in ViewController.m. The build will fail with the master branch. To get it work, checkout the working branch.
The reason that using #import MyModule; worked, is that "modules are a packaging together of the framework executable and it's headers" (from this SO post).
In other words, when you #import MyModule;, Objective-C automatically imports all the swift and Objective-C headers related to the module, while you cannot include the swift header from the umbrella header. I suggest you take a look at the differences between #import and #import in the linked SO answer.
It turns out that using #import MyModule; instead of #import <MyModule/Module.h> allows using walk and swiftWalk methods as expected from the Objective-C app.
I honestly don't understand the details, but I hope this helps someone else. Please feel free to explain!
I'm trying to build a Cocoa Touch Framwork (XCode 6, obviously) with Objective-C and some Swift classes.
I've successfully added Swift classes to regular projects (no framworks) before so I'm aware of the build settings required for this (Embedded Content Contains Swift Code). Unfortunately, the framework project doesn't build the header file (projectName-Swift.h) and I'm not able to import the class and the Swift classes into any of my .m files.
Does anybody know why this wouldn't work, and what build settings I need to change in order to make this work?
Finally found the answer;
Within the frameworks the import statement for Swift files has to look as follows;
#import <ProjectName/ProjectName-Swift.h>
instead of
#import "ProjectName-Swift.h"
Sometimes it helps to RTFM (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82)
I've struggled with this for a while because I don't think the documentation is clear. I have a slightly different project setup, and it's taken me a while to work this out so I thought I'd answer here.
I have a project with name: MyProject
In this project, I have a framework: MyFramwork
I have a swift file in MyFramework which I want to import into an objective c file in MyFramework.
To do this, you need:
#import <MyFramework/MyFramework-Swift.h>
NOT
#import <MyProject/MyFramework-Swift.h>
Hope that helps someone.
I'm trying to build a static library with a bunch of views and other classes...
They deal e.g. with emails, pdfs,... thus I need to link to some of the iOS frameworks like MessageUI and CoreGraphics e.g.
Somehow that doesn't work or I make another mistake.
Although I included those frameworks I get a ton of errors for example CGFloat is an unknown type or UIImage is unknown.
A sample iOS project with all library classes directly included and with the same frameworks linked, works perfectly. But outsourcing those to a framework doesnt work...
I'd appreciate any help! Thanks a lot!
You have to add
#import <UIKit/UIKit.h>
into the precompiled header of your library, as only Foundation is included by default