Objective-c calls Swift
To use Swift in Objective-C, perform the following steps:
1. Create an Objective-C Project:
2. Add a Swift class file:
3. Click Next. In the displayed dialog box, select YES.
4. Generate a YourProject-Bridging-Header.h file,
5. Create a class in SwiftClass as follows:
Import Foundation class SwiftTest: NSObject {func test ()-> NSString {return "test "}}
6. Call the SwiftTest class in ViewController as follows:
# Import "ViewController. h" # import "ObjectiveTest-Swift.h" // here note that it is consistent with the project name "YourProject-Swift.h"
7,ImportAfter that, you must compile it once. Otherwise, the compilation fails.
8,After compilation, you can call the following in the class:
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. swiftTest * tests = [SwiftTest new]; [tests test];}