*****閱讀完此文,大概需要5分鐘******
這個程序不復雜,但是有些問題遇到了可能就會搗弄很久,特此輸出此文,希望可以幫助大家快速解決相應的問題,
一、配置步驟
1、主專案中添加工程名-Bridging-Header.h
這一步可以如其他文章介紹的那樣,先添加Test.swift然后,Xcode會提醒你添加對應的-Bridging-Header檔案的,
2、配置工程
- Build Settings設定Defines Module為Yes
- 設定Podfile,如下:
use_modular_headers!
use_frameworks! :linkage => :static
- Xcode設定Swift版本為最新
二、注意事項以及可能遇到的問題
1、OC中呼叫Swift代碼
import Foundation
@objcMembers
public class MDSwiftDemoViewController : UIViewController {
public override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
func show() {
print("hello swift...")
}
}
注意在對應的Swift類前面添加public修飾
如果在OC的呼叫Swift代碼是夸Pod庫呼叫,需要添加對應的參考如下:
#import <SwiftDemoProject/SwiftDemoProject-Swift.h>
2、NSClassFromString等使用注意
如果在OC代碼中直接NSClassFromString(“MDSwiftDemoViewController”),得到的結果將會是nil,因為在OC中Swift類中的所有類的介面都轉到了一個叫Project_Example-swift.h的頭檔案中,其中的類名和方法實際都已經改變了,已經不是原來的類名了,解決方法如下:
import Foundation
@objc(MDOCSwiftDemoViewController)
@objcMembers
class MDSwiftDemoViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
}
func show() {
print("hello swift...")
}
}
在OC代碼中呼叫MDOCSwiftDemoViewController類即可,
NSClassFromString(“MDOCSwiftDemoViewController”)
3、工程出現”<A/A.h> file not found”報錯
參考解決:
檢查對于的podspec檔案對A的依賴,由于Podfile中配置了static,未添加依賴會導致的頭檔案參考找不到,
4、遇到 “library not found for -lAFNetworking”報錯
參考解決:
Build Setting -> Other Linker Flag 中的參考,找到移除之后的框架,洗掉參考即可
三、參考檔案:
https://www.jianshu.com/p/57fedd74763d
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/295208.html
標籤:其他
