項目開發中OC中會使用到Swift文件,也可能在Swift項目中調用OC文件,兩種方式略有不同:
OC調用Swift文件
1.OC項目中,新建Swift文件會讓選擇創建頭文件,ProductName(項目名稱)-Bridging-Header.h
FlyElephant.png
2.Swift調用需要設置Module為Yes,Product Module Name 默認是項目名稱:
Paste_Image.png
3.項目需要調用Swift文件導入OCDemo-Swift.h文件:
<pre><code>`
import "ViewController.h"
import "OCDemo-Swift.h"
@interface ViewController ()
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Person *person = [[Person alloc] init];
NSLog(@"FlyElephant---%@",person.personName);
}(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end`</code></pre>
Swift調用OC文件
1.新建OC文件會提示創建頭文件:
FlyElephant.png
2.在頭文件中導入新創建的文件即可:
<pre><code>`