ViewController.swift
import UIKit
func oc_u_swift(){}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.swift_OC();
}
//Swift 调用OC
func swift_OC(){
let oc = OCClass()
let i = oc.ocFunc()
}
func oc_u_swift(){
let oc = OCClass()
oc.oc_use_swift()
}
}
OCClass.h
#import <Foundation/Foundation.h>
@interface OCClass : NSObject
-(int)ocFunc;
-(void)oc_use_swift;
@end
OCClass.m
#import "OCClass.h"
#import "Swift_OC_C混编-swift.h"
@implementation OCClass
-(instancetype)init{
if (self = [super init]) {
}
return self;
}
/**
swift 调用 OC 的函数
1、只要在桥接头文件 xxxx-Bridging-Header.h #import OC的头文件可以
*/
-(int)ocFunc
{
int (^p)(int, int);
//把函数赋值给block
p = ^(int a, int b){
return a + b;
};
//使用
int result = p(10,40);
return result; //result = 50
}
/**
OC 调用 swift
1、#import "Swift_OC_C混编-swift.h" ---> Build Setting - "Product Module Name-swift.h"
2、公开函数 func oc_u_swift(){}
3、桥接头文件只适合用于 swift调用OC,这里只要正确引用头文件就可以
*/
-(void)oc_use_swift
{
ViewController *v = [[ViewController alloc]init];
[v swift_OC];
}
GitHub地址:https://github.com/JasonChen02/Swift-OC_varandra https://github.com/JasonChen02/Swift-OC_varandra (第一次上传到github,相互学习)