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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.