OC calls C + +:
1. Create OC Project:
2. Main steps: change a. m file suffix to. mm,
3. Create a C + + file
#ifndef __test__cpphello__ #define __test__cpphello__<stdio.h>class Cpphello {public: Static void hello ();}; #endif /* Defined (__test__cpphello__) */
CPPHello.h
1 void Cpphello::hello () 2 {3 printf ("Hello C + + \ n"); 4 }
CPPHello.cpp
4. Only need to #import the file when the call is done.
C + + calls OC:
1. Create OC files based on the above code
2 . Remove the contents of the. h file and write it as a C + + method (cannot contain OC code) such as:
1 void Objchello ();
ObjcHello.h
3 . Change. m files to. mm Files
4 . Implement the method in the. mm file and invoke the OC code in the method (remember to import the appropriate OC header file: NSLog #import <Foundation/Foundation.h>
1 " ObjcHello.h " 2 #import <Foundation/Foundation.h>34void Objchello ()5 {6 NSLog (@ "Hello OBJC \ n"); 7 }
objchello.mm
or this:
1#import"ObjcHello.h"2#import <Foundation/Foundation.h>3 4 //OC Object5 @interface Objchello:nsobject6 7-(void) Testlog;8 9 @endTen One @implementation Objchello A --(void) Testlog - { theNSLog (@"Hello OBJC \ n"); - } - - @end + - voidObjchello () + { AObjchello *obj =[[Objchello alloc] init]; at [obj Testlog]; -}
objchello.mm
Swift calls C language code
1. Create a swift Project
2. Create C file appears the following interface, select Yes to automatically generate the bridge connector file:< project name >-bridging-header.h
The purpose of the bridge header file is to set up a bridge for Swift to call the C or OC object, which is named "< project name >-Bridging-header.h", with the introduction of a C or OC header file in the bridge header file.
1 #ifndef __testswift__chello__ 2 #define __testswift__chello__34 #include <stdio.h>56void hello (); 7 8 #endif /* Defined (__testswift__chello__) */
CHello.h
1 " CHello.h " 2 3 void Hello () 4 {5 printf ("hello c\n"); 6 }
chello.c
" CHello.h "
Testswift-bridging-header.h
Import Foundationhello ();
Main.swift
Swift calls Oc,oc to call C + +:
1.Swift call OC As with Swift call C operation, you need to #import. h through the bridge header file.
2. Use as follows:
1 #import <Foundation/Foundation.h>23@interface ocobj:nsobject4 5 -(void) testlog; 6 7 @end
ocobj.h
1 #import " ocobj.h " 3 @ Implementation Ocobj 4 5 -(void ) Testlog { 7 NSLog (@" hello oc\n ); " 9 10 @end
OCOBJ.M
1 Import Foundation 2 3 var obj = ocobj (); 4 obj.testlog ();
Main.swift
3.swift can not call C + + code directly, only by calling Oc,oc call C + + OC Call C + + as long as the OC point. m file is most changed to. mm, step reference above OC call C + +
If you want to add a bridge file manually, please refer to the figure below
Swift, OC, C + + mixed programming