Swift, OC, C ++ hybrid programming, swiftoc
OC calls C ++:
1. Create an oc project:
2. Main steps: Change the suffix of A. m file to. mm,
3. Create a c ++ File
# Ifndef _ Test _ CPPHello __# define _ Test _ CPPHello __# include <stdio. h> class CPPHello {public: static void hello () ;};# endif/* defined (_ Test _ CPPHello __)*/CPPHello. h1 void CPPHello: hello () 2 {3 printf ("Hello C ++ \ n"); 4}CPPHello. cpp
4. You only need to # import the file when calling it.
C ++ calls OC:
1. Create an OC file based on the above Code
2. Remove the. h file content and write it as a c ++ method (the oc Code cannot be included), for example:
1 void objcHello ();ObjcHello. h
3. Change the. m file to the. mm file.
4. Implement the method in the. mm file and call the oc code in the method (remember to import the corresponding oc header file, such as NSLog-> # import <Foundation/Foundation. h>
1 # import "ObjcHello. h" 2 # import <Foundation/Foundation. h> 3 4 void objcHello () 5 {6 NSLog (@ "Hello objc \ n"); 7}ObjcHello. mm
Or:
1 # import "ObjcHello. h "2 # import <Foundation/Foundation. h> 3 4 // OC object 5 @ interface ObjcHello: NSObject 6 7-(void) TestLog; 8 9 @ end10 11 @ implementation ObjcHello12 13-(void) testLog14 {15 NSLog (@ "Hello objc \ n"); 16} 17 18 @ end19 20 void objcHello () 21 {22 ObjcHello * obj = [[ObjcHello alloc] init]; 23 [obj TestLog]; 24}ObjcHello. mm
C language code called by Swift
1. Create a swift Project
2. The following page is displayed when the c file is created. Select yes to automatically generate the bridging header file: <Project name-Bridging-Header.h
The purpose of bridging the header file is to build a bridge for Swift to call C or OC objects. Its name is "<Project name>-Bridging-Header.h ", introduce the C or OC header file in the bridge header file.
1 # ifndef _ TestSwift _ CHello _ 2 # define _ TestSwift _ CHello _ 3 4 # include <stdio. h> 5 6 void hello (); 7 8 # endif/* defined (_ TestSwift _ CHello __)*/CHello. h1 # include "CHello. h" 2 3 void hello () 4 {5 printf ("hello C \ n"); 6}CHello. C # import "CHello. h"The TestSwift-Bridging-Header.Himport Foundationhello ();Main. swift
Swift calls OC and OC calls C ++:
1. Swift calls the same oc as Swift calls the c operation. You need to use the bridge header file # import. h to call
2. Use as follows:
1 # import <Foundation/Foundation. h> 2 3 @ interface OcObj: NSObject4 5-(void) testLog; 6 7 @ endOcObj. h 1 # import "OcObj. h "2 3 @ implementation OcObj 4 5-(void) testLog 6 {7 NSLog (@" hello OC \ n "); 8} 9 10 @ endOcObj. m1 import Foundation2 3 var obj = OcObj (); 4 obj. testLog ();Main. swift
3. swift cannot directly call c ++ code. It can only call oc and oc to call c ++ as long as the oc point is called. the m file is last changed. mm. For the procedure, refer to the above oc call c ++
If you want to manually add a bridge file, refer to the figure below
OC calls Swift code
When you create a project above xcode6.0, for example, the project name is Test, there is a hidden file in the project: Test-Swift.h, you can call the file through
# Import "Test-Swift.h", then you can call classes in swift or other
For manual modification, refer to (not recommended ):