進度條

[Objective C] 使用Swift檔案

更新日期:

以下為在Objective C 檔案中使用Swift Class的方法

Apple 文件
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

 

Stackoverflow

https://stackoverflow.com/questions/26328034/importing-project-swift-h-into-a-objective-c-class-file-not-found

重點:

1. #import "ProductModuleName-Swift.h 只能放在.m檔

2. .h 檔放 @class ClassName 

 

 

 

a. project target -> build settings -> search  "Objective-C Generated Interface Header Name"
   可以找到import 的名稱, 例如UnitTest-Swift.h
(需要先有加入混合資料?? 不然 Objective-C Generated Interface Header Name 欄位不會出現。
刪掉swift檔也會讓他消失)。
b. 在object-c class import該名稱即可使用。


// 如果是只有一個 main target
如果要在unit test 裡面讓objective-c test file 讀的到 swift file
需要在Test 的target -> build settings -> Search Paths -> Header Search Paths 加入下面路徑
"$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources"
來源:https://stackoverflow.com/questions/26473058/ios-myproject-swift-h-file-not-found-when-running-unit-tests-for-swift

// 如果是其他的 framework 或 lib
因為已經做成Lib了
所以先在"YourLibName.h" 裡面加入 UnitTest-Swift.h(或你的名稱)
所以只需要#import "YourLibName.h" 就可以