最近一時手癢,想說 Swift 可以與 Objective-C 混合使用,立馬參照官方作法Swift and Objective-C in the Same Project以及Swift Type Compatibility
當我使用純Swift物件時,執行狀況並不如預期順利,在 Stack Overflow 的這篇提供了相當不錯的解法
How to call Objective C code from Swift
Using PURE Swift Classes in Objective-C
Step 1 Create New Swift Class
1 | import Foundation |
Step 2 Import Swift Files to ObjC Class
In SomeRandomClass.m
:
#import "<#YourProjectName#>-Swift.h"
Step 3 Use your pure swift class
1 | PureSwiftObject * pureSwiftObject = [PureSwiftObject newInstanceNamed:@"Janet"]; |