Swift calls C, C + +, Object
1.Swift calling C language
A, first add the CFile file in the project named Chello, and generate the bridge file.
b, after the creation of the project structure
b, write the interface method in the CHello.h file, write the implementation method in CHELLO.C, and finally introduce the CHello.h in the Swiftcallc-bridging-header.h file.
CHello.h file
#ifndef __swiftcallc__chello__#define __swiftcallc__chello__#include <stdio.h>void SayHello (); #endif/* Defined (__swiftcallc__chello__) */
CHELLO.C file
#include "CHello.h" void SayHello () { printf ("hello\n");}
Swiftcallc-bridging-header.h file
#import "CHello.h"
2.Swift Calling OC language
A, in the same vein, add the Object-c file under the project named Ochello. This is not needed here because a file bridge has been created.
b, write the interface method in the OCHello.h file, write the implementation method in the OCHELLO.C, and finally introduce the OCHello.h header file in the Swiftcallc-bridging-header.h file
OCHello.h file
#import <Foundation/Foundation.h> @interface ochello:nsobject//method No parentheses, really not fit--!-(void) SayHello; @end
OCHELLO.M file
#import "OCHello.h" @implementation ochello-(void) sayhello{ NSLog (@ "Hello");} @end
Swiftcallc-bridging-header.h file
#import "CHello.h" #import "OCHello.h"
C, the last call to Object-c in Swift code is the ~
Import Uikitclass Viewcontroller:uiviewcontroller { override func Viewdidload () { super.viewdidload () // Additional setup after loading the view, typically from a nib. var oc = Ochello () Oc.sayhello () } override Func didreceivememorywarning () { Super.didreceivememorywarning () //Dispose of any resources, can be recreated. }}
3.Swift calling the C + + language
Call C + + code as long as you change the original ochello.m file to ochello.mm, and then you can reference the iostream header file in the file .... ^^ instantly have C + + feel!
Finally, a slight modification of the implementation of the method is possible.
#import "OCHello.h" #include "iostream" using namespace std; @implementation ochello-(void) sayhello{ cout<< " Hello "<<ENDL;} @end
iOS Development--practical technical article Swift&swift call C, C + +, Object