Swift文件:main.swift
import Foundation //Swift调用C函数 desc1() //Swift调用OC //拿到OC类 var funcclass = Root() funcclass.desc2()
OC文件:Root.h
#import <Foundation/Foundation.h> @interface Root : NSObject -(void)desc2; @end
Root.m
#import "Root.h"
@implementation Root
//求和函数
//1、定义函数
int sum2(int a,int b)
{
return a+b;
}
-(void)desc2
{
//2、声明Block
int (^p)(int,int);
//3、函数指针指向函数
// p = sum2;
p = ^(int a,int b) //把函数赋值给Block
{
return a+b;
};
//4、使用
int result = p(10,40);
printf("OC方法输出result:%d\n",result);
}
C函数文件:
Fun.c
#include <stdio.h>
//求和函数
//1、声明函数
int sum1(int a,int b)
{
return a+b;
}
void desc1()
{
//2、声明函数指针
int (*p)(int,int);
//3、函数指针指向函数
p = sum1;
//4、使用
int result = p(10,20);
printf("C函数输出结果:%d\n",result);
}
桥接文件:工程名称-Bridging-Header.h
//这里面需要导入 桥接的C或OC的头文件 //导入C函数 void desc1(); //导入OC头文件 #import "Root.h"