zoukankan      html  css  js  c++  java
  • Swift调用OC和C

    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
    ", 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
    ", result);
    }

    桥接文件:project名称-Bridging-Header.h

    //这里面须要导入 桥接的C或OC的头文件
    
    //导入C函数
    void desc1();
    
    //导入OC头文件
    #import "Root.h"


  • 相关阅读:
    2014-11-1 NOIP模拟赛2
    洛谷P1014 Cantor表
    洛谷P1011 车站
    洛谷P1013 进制位
    2014-11-1 NOIP模拟赛1
    2017-9-20 NOIP模拟赛
    洛谷P2016 战略游戏
    洛谷P3182 [HAOI2016]放棋子
    2014-10-31 NOIP模拟赛
    洛谷P1736 创意吃鱼法
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/6709470.html
Copyright © 2011-2022 走看看