Swift calls OC and C

Source: Internet
Author: User

Swift file: Main.swift

Import Foundation//swift call C function Desc1 ()//swift call oc//get OC class var funcclass = Root () funcclass.desc2 ()

OC File: Root.h

#import <Foundation/Foundation.h> @interface root:nsobject-(void) desc2; @end

Root.m

#import "Root.h" @implementation root//summation function//1, define function int sum2 (int a, int b) {    return a+b;} -(void) desc2{    //2, declaring block    int (^p) (int, int);        3, function pointer pointing function    //    p = sum2;    p = ^ (int a, int b)//Assign the function to block    {        return a+b;    };        4. Use    int result = P (ten, +);    printf ("OC method Output result:%d\n", result);}

C Function File:

Fun.c

#include <stdio.h>//summation function//1, declaring function int sum1 (int a, int b) {    return a+b;} void Desc1 () {    //2, declaring function pointer    int (*p) (int, int);        3, function pointer pointing function    p = sum1;        4. Use    int result = P (ten, +);    printf ("C function output Result:%d\n", result);}

Bridging files: Project name-bridging-header.h

This needs to import the bridged C or OC header file//import C function void Desc1 ();//import OC Header file #import "Root.h"


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift calls OC and C

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.