在Objective-C中使用Swift,步骤相似:
1、首先创建一个Objective-C的项目:
![](http://upload-images.jianshu.io/upload_images/144560-49486e0ee7175f0c.png?</p><p>imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Paste_Image.png
2.加入一个Swift的类文件:
![](http://upload-images.jianshu.io/upload_images/144560-0d57de2c34a2416e.png?</p><p>imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Paste_Image.png
3、点击Next,弹出一个对话框,选择YES
![](http://upload-images.jianshu.io/upload_images/144560-c078582d708466c9.png?</p><p>imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Paste_Image.png
4、生成一个YourProject-Bridging-Header.h文件。在本项目中生成的文件例如以下图:
![](http://upload-images.jianshu.io/upload_images/144560-d3b5f461d09a4203.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Paste_Image.png
5、在SwiftClass创建类例如以下:
import Foundation
class SwiftTest : NSObject {
func test()->NSString{
return "test"
}
}
6、在ViewController调用SwiftTest类例如以下:
#import "ViewController.h"
#import "ObjectiveTest-Swift.h" //这里注意和项目名称一致 "YourProject-Swift.h"
7、import之后一定要先编译一次,否则编译出错。
8、编译之后就能够在类里面调用了例如以下:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SwiftTest *tests = [SwiftTest new];
[tests test];
}