I have Test target for my framework project which include:
Obj-C tests
Swift tests with #testable import to test internal classes and functions
Some utils written in Obj-C for my tests which should be used in both 1) and 2)
Since I want to use Obj-C utils I should add Bridging header to my test target to have access from swift sources. The problem is that #testable import doesn't work with Bridging header simultaneously: as long as I don't use Bridging header I have access to all internal API from my swift test sources, but when I add Bridging header I have only access to public API.
Has anyone experienced that? Is #testable directive implemented using Bridging header which makes using both simultaneously impossible? Thoughts on how to resolve the issue?
UPDATE:
Apparently problem appears when you're importing the same framework as #testalbe in your swift code and using #import in Obj-C in your bridging header (directly or in one of included headers). I can manage my code to avoid importing framework simultaneously from Bridging header and swift, though general problem is not resolved
Related
I am trying to utilize an Objective-C framework inside the Swift package. For this, I would need to import the headers something like
#import<Framework/header.h>
Traditionally this can be achieved via bridging header. How can I set the bridging header via package manager settings?
This is bit tricky. You cannot import Objective-C framework's header files directly into SPM as you cannot write import statement such that #import<Framework/header.h>
The only feasible way is, write swift interface for your header files in Framework and then you would be able to import all methods. It can be done with module map as referred here
I have an umbrella header called
MyApp-Swift.h
I've imported the following class "AFHTTPSessionManager.h" in the umbrella header.
I made the umbrella header public in build phases and I also made sure to set Define modules to yes.
However, in my swift framework I am still getting the following error
Use of undeclared type 'AFHTTPSessionManager'
I don't know why this is continuing. I thought swift would automatically pick up my Objective-C import. Any tips or suggestions are appreciated.
I tried following this source
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82
,but it didn't work for me. This is very fusterating.
This is the swift code that causes the error.
import Foundation
class MyApp {
private var manager: AFHTTPSessionManager?
}
The thing is, brigding header doesn't work for Framework targets. Solution is to create module map target to build module map for needed ObjC framework.
Here is example for CommonCrypto: https://stackoverflow.com/a/42852743/1840136; in your case difference should be that script line
header "${SDKROOT}/usr/include/CommonCrypto/CommonCrypto.h"
will be replaced with path to AFNetworking headers (I'm not sure if you're placing 3rd party libraries just within project or getting from pods, so check by yourself).
I have a project with large amount of obj-c and swift code inside it.
I have an obj-c class which imports ProjectName-Swift.h to let that class access swift code.
Now I need to expose that obj-c class to the swift so I can access it from swift code.
The problem is that after import in bridging header name of obj-c class project stops building with error that it cannot find ProjectName-Swift.h which import is stated in obj-c class.
I cannot remove import of ProjectName-Swift.h from obj-c class because after that class breaks.
What can I do ?
OK, had one answer and then re-read the question. Make absolutely certain that your import of the swift header is the Module Name of the project (not necessarily the project name):
Apple Documentation: Swift and Objective-C in the Same Project
More specifically, go to the section marked "Importing Swift into Objective-C" and read it. And even more specifically:
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 .m file within that framework target using this syntax and substituting the appropriate names:
#import <ProductName/ProductModuleName-Swift.h>
I'm building a mixed language framework. I mainly have Swift files, and a few Objective-C ones.
One of the Objective-C files is a crypto class that uses CommonCrypto.
It seems that I can't import it for some reason, even though that I can import it in a Objective-C framework.
Can someone explain to me why that is?
All the other solutions that I found talk about how to use CommonCrypto in Swift when I need to use it in Objective-C in a Swift framework.
P.S:
I tried adding the import in the umbrella header file like so:
#import <CommonCrypto/CommonCrypto.h>
the error: Include of non-modular header inside framework module 'name of header'
This answer did not fix the problem: answer
I've encountered this very problem myself. Here's how you resolve it:
Create a module map file (here's my file).
Copy the latest CommonCrypto.h header.
Create a directory CommonCrypto for both these files.
Copy the directory (via drag-and-drop) to your project.
Add the directory path under SWIFT_INCLUDE_PATHS for your target framework.
This should allow you to use import CommonCrypto wherever you want (for Swift, not Objective-C).
Edit: Seems like I misread the question initially. You want to use CommonCrypto in Objective-C and then use that from Swift. Here's some advice: don't #import CommonCrypto in your public headers, but rather just internally. Wrap all your crypto-structures so that there's no public dependency for CommonCrypto whatsoever, and then just use it from Swift via the default bridging procedure.
I try to import RNCryptor to my project to encrypt some string. But I have trouble to understand how to import that to my objective-c project. I copied the RNCryptor/RNCryptor.swift and RNCryptor.h by drag and drop in to my project. Then it "created a bridging header". After that I tried to debug but I have 38 error messages like: Use of unresolved identifier 'kCCKeySizeAES256' or Use of unresolved identifier 'CCPBKDFAlgorithm'. How can I fix that and how would I use it in a viewController.h file? Normally with #import "RNCryptor.h"
https://github.com/RNCryptor/RNCryptor#obj-c
I think you need to add #import "RNCryptor/RNCryptor.h" to the bridging header file, did you do that?
See the RNCryptor Swift branch
The Bridging Header
CommonCrypto is not a modular header in Xcode 7. This makes it very challenging to import into Swift. To work around this, the necessary header files have been copied into RNCryptor.h, which needs to be bridged into Swift. You can do this either by using RNCryptor as a framework, adding #import "RNCryptor/RNCryptor.h" to your existing bridging header, or making RNCryptor/RNCryptor.h your bridging header in Build Settings, "Objective-C Bridging Header."