How a bridging header works in Xcode?

814 Views Asked by At

I'm curious about how Xcode actually works with C libraries and Swift 4.

I've added my C library by creating bridging header, after then I could use my C functions without any efforts. Is Swift built upon C language?

2

There are 2 best solutions below

0
Nishant Pathania On

When you add a library in C in the Xcode you enter the name and check the also create header file box, Xcode will ask you about the Objective-C bridging header file. Just create it. The name of this file is tricky, because it also supports other C family languages, like pure C or C++, Objective-C and C plus-plus.

Not entirely Swift is built on C language but Objective . C as well earlier Swift Support was not there. .h files which is a header that helps us to bridge the library and use it in our project which runs on Swift.

Have a Look at the Documentation by Apple:-- https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift

0
Cristik On

Unlike Objective-C, or languages in the C/C++ family, Swift doesn't have the concept of headers for public interface, instead the accessibility is declared via the accessibility modifiers (private/internal/public/etc).

The only exception is the bridging header for apps, and the umbrella header for libraries. The compiler knows to look in these locations for declarations that can be imported into Swift (not all C content in a header can be made available for Swift).

Once it find the usable symbols, the compiler will make them available for all Swift files, and will generate the necessary calling code.