“工程名-Swift.h”這個文件是將Swift代碼轉(zhuǎn)為OC的文件,打開這個文件查看,可以看到全部是OC代碼。例如,我在OC項(xiàng)目中,創(chuàng)建一個Swift文件,繼承自UIView,命名為SwiftView。
自動生成的代碼:
import UIKit
class SwiftView: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
build一下,查看“項(xiàng)目名-Swift.h”文件(在OC文件里,#import "工程名-Swift.h",然后跳到頭文件查看,直接在工程導(dǎo)航欄里是找不到的),會在文件的底部看到:
@interface SwiftView : UIView
- (nonnull instancetype)initWithFrame:(CGRect)frame OBJC_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder * _Nonnull)aDecoder OBJC_DESIGNATED_INITIALIZER;
@end
這個說明,系統(tǒng)會把Swift文件轉(zhuǎn)換為OC文件,放到“工程名-Swift.h”文件中,以供OC調(diào)用。
這里有一點(diǎn)要注意,“工程名-Swift.h”文件里的工程名到底是什么?實(shí)際并不是我們真正的工程名:

工程名
而是:
target->build setting->packaging->product module name
將product module name換成“product module name-Swift.h”。如果你的product module name有問題,例如是中文,特殊字符,你需要改為合法字符。改過之后,需要刪除DerivedData文件夾,重新編譯。
/Users/用戶名/Library/Developer/Xcode/DerivedData