iOS Development--practical technical article Swift&swift call C, C + +, Object

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.