OC/Swift/C/C++ mixed programming posture

1. OC calls C language methods
1. The .m file in OC is fully compatible with the C language, and the C header file can be directly imported for use
2. Define a .c C language file, import it in the .m file, and you can use it.
 
Second, OC calls C++ language methods
1. You need to add an m after the .m file and change it to a .mm file. The .mm file is fully compatible with C/C++ and can be used by importing the C/C++ header file
2. Define a .cpp C++ file and import it in the .mm file to use it.
 
 
Third, C++ calls OC language methods
1. In the defined OC class, if you want the C++ file to enter the OC file successfully, you need to delete all the methods involved in the OC in the .h file of the OC class. Then define the method name of the C/C++ language in the .h file. It can be implemented in OC/C/C++ language in .mm file.
 
 
Fourth, swift calls the C language method
The swift language cannot use the C language method directly, it needs to be used through the bridging header file.
1. Create a C file under the swift project and generate the bridging-header bridging header file.
2. Declare the method name in the C language .h file, implement the method in the .c file, and import the C language file in the bridge file #include "ah"
3. Use in swift file
 
Five, swift calls C++ language methods
1. Create an OC file under the swift project and generate the bridging-header bridging header file.
2. Declare the method name in the OC language .h file, implement the method in the .mm file, and import the C language file in the bridge file #include "ah"
3. Use in swift file
 
 
Six, bridging-header bridging header file description
The role of the bridging-header bridging header file is to allow the swift language to execute the methods of the oc language.
So it is the introduction of the oc class #include "oc.h"
At the same time, the system will also create an invisible bridge file for oc to call the swift language method.
This bridging file is: project name-Swift.h.
You can import this header file in the oc class and use the method in the swift class (now you can use it without importing this file, it is estimated that Xcode has optimized it).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324673066&siteId=291194637