Tips:swift项目和oc项目中swift和oc混编做法一样
productName
本文中项目名为Test,即Build Settings中Product Name配置为Test
一、swift中调用oc类
- 1.创建桥接文件 Test-Bridging-Header.h,检查Build Settings中Objective-C Beidging Header配置Test/Test-Bridging-Header.h
- 2.在桥接文件Test-Bridging-Header.h中#import MyView.h
- 3.直接在swift类中按照swift语法使用MyView即可
//Test-Bridging-Header.h
#import "MyViewh"
import UIKit
class TestViewController: UIViewController {
func testOC(){
let tmpV = MyView()
tmpV.test1()
self.view.addSubview(tmpV)
}
}
二、oc中调用swift类
- 1.在oc类中 #import "Test-Swift.h",检查Build Settings中Objective-C Generated Interface Header Name配置Test-Swift.h
- 2.直接在oc类中使用oc语法调用swift类
#import "MyView.h"
#import "Test-Swift.h" //ProductName-Swift.h
@implementation MyView
- (void)test1{
testSwiftView *view = [[testSwiftView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
view.backgroundColor = [UIColor redColor];
[self addSubview:view];
}
@end
网友评论