码迷,mamicode.com
首页 > 编程语言 > 详细

Swift调用OC和C

时间:2015-07-09 16:15:31      阅读:129      评论:0      收藏:0      [点我收藏+]

标签: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

标签:c   oc   swift   桥接   调用   

原文地址:http://blog.csdn.net/pengyuan_d/article/details/46816859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!