在OC專案中建立一個swift檔案的時候,Xcode 會提示 需要建立一個橋接檔案,點確定建立橋接檔案,Xcode會自動建立一個橋接檔案,如下圖
名字:工程名-Bridging-Header.h
這個橋接檔案是 swift 呼叫OC檔案的時候用的

設定Defines Module為YES
設定 Product Module Name ,也可以不設定,預設為工程的名字。這個在後面會用到
1、OC呼叫Swift
在OC需要用到的swift檔案中 匯入檔案 <Product Module Name -Swift.h> 因為 Product Module Name 預設是工程的名字
所以直接匯入 #import <工程名-Swift.h>
OC 調 Swift 程式碼
//
// ViewController.m
// SwiftInOc
//
// Created by Z_z_z on 2018/5/21.
// Copyright © 2018年 zzz. All rights reserved.
//
#import "ViewController.h"
#import <SwiftInOc-Swift.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SwiftController * swift = [[SwiftController alloc]init];
[swift log];
}
複製程式碼
log

2 Swift呼叫OC
在橋接檔案中新增需要呼叫的OC.h檔案。如圖

Swift調OC程式碼
//
// ViewController.swift
// OcInSwift
//
// Created by Z_z_z on 2018/5/21.
// Copyright © 2018年 zzz. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let ocVC = OcController();
ocVC.log();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
複製程式碼
log
