Swift調用OC和C

來源:互聯網
上載者:User

標籤:c   oc   swift   橋接   調用   

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"


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Swift調用OC和C

相關文章

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.