Swift objects not visible only when compiling to a tvOS target - ios

So I've got an application with multiple targets, some iOS and some tvOS. It's originally written in Objective-C, but we write any new functionality/code in Swift.
The problem is that when building for a tvOS target, the Swift classes don't get added to the -Swift.h file (the one that is generated when building a project with both Objective-C and Swift source files) properly. Thus, the Objective-C code doesn't recognize the Swift classes, and I get build errors.
The project builds fine, and all Swift classes are visible when building an iOS target.
Do any of you have any idea as to what might be causing this?

I figured it out. Evidently, even though the system does create a -Swift.h file, it won't actually bridge the classes unless the Objective-C-Bridging-Header.h has a value pointing to a bridging header in the project, despite the fact that the Objective-C Generated Interface Header Name plist key had a value. It would generate the Swift bridging header just fine--it just wouldn't actually do any of the bridging.

Related

Swift Compiler Error: Cannot find 'ClassName.h' in scope - Xcode 13

For starters, this only became an issue after upgrading to Xcode 13. I have an app that was written in Objective-C and am utilizing some files written in Swift. In order to access classes that were created in the app in Objective-C, I do have a bridging-header named TargetName-Bridging-Header.h, where all of the classes in question are being imported like so:
#import "ClassName.h"
In the target Build Settings, Objective-C Bridging Header's value is set to the path of the bridging-header file (ProjectName/TargetName-BridgingHeader.h), and the app seems to have no problem finding it. Precompile Bridging Header is set to Yes. Install Objective-C Compatibility Header is set to Yes. Furthermore, the app builds and runs and seems to have no problem accessing those Objective-C built classes, yet the compiler continues to throw errors stating that it cannot find those classes. I've tried deleting derived data and cleaning. What am I missing? Is this perhaps a new Xcode bug that anyone else has experienced?
The exact error is "Swift Compiler Error: Cannot find 'ClassName.h' in scope"
Had the same problem, I removed the specific import statement from the bridging header, put it back and saved...next compilation went smooth without any errors. weird bug...but avoidable..

Converting an Objective-C framework to Swift - Header files still displaying Objective-C functions

I converted my entire Objective-C framework to Swift. After building it, when I check the framework's Header files, it is still displaying the old Objective-c functions.
I deleted all the Objective-C files from the framework project. I have tried cleaning and building the project. But with no success.
You deleted the Objective-C files from the Xcode project, but did you also remove them from the file system and any source control you might be using?
Also, your Build Settings in your Xcode project can be searching for library/header files even after you've removed them from explicit inclusion in the Project Navigator in Xcode.
Just make sure the search paths don't look for old Obj-C stuff. Be careful of recursive searches like MyXcodeProject/**. The ** will recursively search all subfolders and their subfolders for things to include.
Better to specify folders directly in the search paths if you can. If you know you don't need search paths, you can just clear them all.
...although, you may want to leave the default exclusions in place:
Remember that you can't subclass a Swift class in Objective-C.
Therefore, the class you migrate can't have any Objective-C
subclasses.
Once you migrate a class to Swift, you must remove the corresponding
.m file from the target before building to avoid a duplicate symbol
error.
To make a Swift class available in Objective-C, make it a descendant
of an Objective-C class.
Command-click a Swift class name to see its generated header.
Option-click a symbol to see implicit information about it, like its
type, attributes, and documentation comments.
from: https://developer.apple.com/documentation/swift/migrating_your_objective-c_code_to_swift
Also, delete your DerivedData, do a clean build and restart the xCode.
How can you see "Header" files in Swift based framework even after removing Obj C files. Definitely there's the build issue. If build is proper, then you are not referring/using to the right framework (in the app) after building it. This is most common mistake that I observed with many developers.
Maybe this can help, try it on your framework's Target Build Settings:
Disable modules (Enable modules (C and Objective-C))
Make sure the Swift version is set (Swift Language Version)

Adding unit tests for an existing objective c + swift project

I have an existing iOs project which I would like to add a unit test target to. The classes I'd like to test are both in objective c and swift.
I've managed to create a test target which allows me to test swift only code by adding the implementation swift files to the test target.
However, as soon as I import or use a class that imports objective c code, I run into Linker issues when building the test target:
...
Symbols not found for architecture x86_64
I've tried adding the objectivec mm files to my target which gets my passed the linker error, but I then get an unresolved identifier error for the class I'm importing.
I'm using xcode 9 and swift 3.
edit: I think this may have something to do with the fact the swift bridging header is not available in the test target, however I'm not sure how to add it.
Your test project is a separate target and should have all files it relies upon to be tested linked separately. So first of all click one of the .m files it's missing and check if the test project is also included in the targets. If this is the case there might be a problem with the bridging header your test project uses. Figure out which one it uses from the build settings of your target and see if it includes the same files as your main project.

Objective-C Bridging Header section not found

I'm working on a React Native app and I'm trying to use a module called react-native-socketio.
In order to proceed with the app, I will need to bridge the objective C header in Xcode but the related section is missing.
According to the tutorial
I have also noticed that the section Swift Compiler – Code Generation is also missing from Xcode.
Why?
I have resolved this matter by first adding a new swift file to the project, then the respective section appeared.
As you can probably tell, I now have an empty Swift file just sitting inside my project folder and options to bridge my header file.
Thanks for the help everyone.
Looks like you have created an ObjectiveC project instead of Swift (I can see the AppDelegate.h & AppDelegate.m). ObjC project wont have Swift Compiler section. Select Swift as language while creating project

Objective-C bridging issue. How do I use an Obj-C framework in a Swift project?

I must have started from scratch about 4 times already. I've followed the solutions listed below but I still have an issue (which I think has something to do with the bridging header file).
Note: I have tried manually creating the bridging header as well as the automated solution Xcode offers when you drag some Objective-C files into a Swift project.
Swift Bridging Header import issue
Connect Objective C framework to Swift iOS 8 app (Parse framework)
Here are the main errors I am seeing. I've tried moving the header file up a level/down a level and it still claims to not see it. Everything is currently where Xcode put it when I selected "Yes" when prompted to created the bridging header automatically. You can also see the full contents of my bridging header.
The "Cannot find protocol declaration for NSObject" error usually refers to circular references problems.
I'm wondering why you put all those standard Apple frameworks in the bridging header? This might be the problem. This special file is supposed to "bridge" Swift and Objective C worlds together, so if you've already referenced and linked your app against those frameworks in your Swift code, you shouldn't need to do it again in the bridging header.
Try to remove all Apple-provided frameworks from your bridging header and only leave the specific ones (IBM....h), and see if it works?
If it doesn't, then start with Foundation/Foundation.h only...

Resources