Swift compiler error when adding ObjC .h to bridging header - objective-c

I am getting a weird error on my application when trying to add an ObjC .h file to the bridging header. In particular, when I try to add the header for STXFeedPhotoCell.h, the project fails to compile with the following error (this error is displayed in STXFeedPhotoCell.h. Here is my bridging header:
#import "NetworkHelper.h"
#import "ArtistModel.h"
#import "UIImageView+Masking.h"
#import "CommentModel.h"
#import "LoginViewController.h"
#import "STXFeedPhotoCell.h"
And here is the error thrown:
/pathToProject/Helden der Volksmusik/STXDynamicTableView/Cells/STXFeedPhotoCell.h:12:9: 'NSDate+DateTools.h' file not found
Indeed, STXFeedPhotoCell.h, has an import statement for file NSDate+DateTools.h.
If I remove line #import "STXFeedPhotoCell.h" from the bridging header, then everything compiles fine.
Would appreciate any pointers on why the compiler is getting this error, or if there is a problem in the way these dependencies are being managed for swift (I am using cocoa pods with frameworks enabled).

To import DateTools and use it in Swift, you have to add this to your bridging file:
#import "DateTools/NSDate+DateTools.h"

Related

When do we need to add frameworks to a Xcode project?

I am confused by this. When I declare my view controller as .m file,
#import <AVFoundation/AVFoundation.h>
#import <Speech/Speech.h>
These two lines in ViewController.h won't cause linking issue. I didn't add any frameworks to my Xcode project.
But when I change it to .mm file because I need to use a C++ library, I got linking issue for the AVFoundation library, and I need to manually add it to my project General -> Link Frameworks and Libraries. Why? Why doesn't this issue appear when I am compiling an .m file?

Objective-C Class Not Found In Swift Bridging Header

I am updating our Cordova framework and I am stuck on getting a particular Objective-C plugin class to work.
In my old (un-updated) project everything works just fine. I have a BridgingHeader.h file, its properly referenced in the build settings. However in my new updated project it can no longer find only one Objective-C class (PushPlugin.m).
What's strange is its finding all other Objective-C classes just fine, its just the PushPlugin.m it can't find.
Here is my BridgingHeader:
#import <Cordova/CDV.h>
#import <objc/runtime.h>
#import <Parse/PFObject.h>
#import <Parse/PFSubclassing.h>
#import <Parse/Parse.h>
#import <objc/message.h>
#import "GPUImage.h"
#import "PushPlugin.h" // This one is not found
#import "CDVParsePlugin.h"
And when I try to use PushPlugin I get not found error:
//AppDelegate.swift
let pushHander:PushPlugin = getCommandInstance("PushPlugin") as! PushPlugin
// ERROR: PushPlugin not found
To be extra certain, the PushPlugin file is referenced in the Compile Sources. And my BridgingHeader is properly referenced in the build settings.
Do you have any idea why one specific Objective-C class is not being found?
Update
So to be clear, there is no error reporting the BridgingHeader file. Its only when I try to use the PushPlugin that I encounter the error (meaning its not importing properly).
As suggested I ran a commandline build I got a build failed with these reasons:
** BUILD FAILED **
The following build commands failed: CompileSwift normal arm64
/Users/user/Desktop/mobile-cordova-upgrade/app/platforms/ios/App/Classes/AppDelegate.swift
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
(2 failures)

Include Prefix.pch in the bridging header?

Having a project that uses a Prefix.pch file to import some commonly used frameworks, for example:
// Prefix.pch
#import UIKit;
When a objective-c header that uses the included prefix frameworks is added to the bridging header compilation fails by not finding the classes imported by Prefix.pch:
// Bridging-Header.h
// UIKit will not be found and will cause errors
#import "FileThatUsesUIKit.h"
So far the cleanest solution found is to also include the Prefix.pch file before the rest of the includes, so that definitions in the prefix are available just like it is expected:
// Bridging-Header.h
#import "Prefix.pch"
#import "FileThatUsesUIKit.h"
Alternatively the specific framework can be imported into the Bridging-Header.h file directly, but that involves keeping on par the definitions between both the prefix file and the bridging header:
// Bridging-Header.h
#import UIKit;
#import "FileThatUsesUIKit.h"
Is importing the Prefix.pch file into the Bridging-Header.h file the correct approach? Are there any other alternatives?

UIKit.h and Foundation.h in Objective-C

If you import UIKit.h does that also automatically import Foundation.h?
UIKit.h doesn't explicitly include it, but I wouldn't be surprised if one of the other UIKit headers does.
However, all of your files will have it anyway, because your default pch (precompiled header, or the header that's automatically added to every file in your project) comes with this:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
This means that every file in your iPhone app will automatically have Foundation and UIKit imported.

Prefix.pch error in Xcode

I get the following error in Xcode, when building project. Can someone help me fix it?
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
with this error:
Prefix.pch:6:38: error: Foundation/Foundation.h: No such file or directory
Prefix.pch:7:28: error: UIKit/UIKit.h: No such file or directory
Sounds like you're not linking against the frameworks correctly, or more likely aren't copying the headers somehow. Likely you accidentally changed the include path of the project.
You have to include the Frameworks:
UIKit.framework
Foundation.framework