加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift调用OC和C

发布时间:2020-12-14 02:02:35 所属栏目:百科 来源:网络整理
导读: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"@i

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:%dn",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函数输出结果:%dn",result);
}

桥接文件:工程名称-Bridging-Header.h
//这里面需要导入 桥接的C或OC的头文件

//导入C函数
void desc1();

//导入OC头文件
#import "Root.h"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读