If you already have an old iOS app, it is written in objective-C, and some of its new features need to be written in swift, then you can call swift from objective-C.
Objective-C does not need to bridge the header file when calling swift, but needs the header file generated by xcode. This type of file is generated by xcode and does not require maintenance. It is invisible to developers. As shown in, it can expose the classes in swift to objective-C. Its name is: <Project Name>-Swift. h. We need to introduce this header file into the objective-C file, and the class in swift needs to be declared as @ objc.
Create an objective-C Project
To better introduce mixed call, we first create an objective-C Project. For the sake of simplicity, we can create a Mac OS X command line project.
Start xcode 6, and click File → new → project menu, on the open choose atemplate for your new project page, select the "OS X → application → command line tool" Project template (as shown in ).
Click "Next". The page shown in is displayed.
Enter "helloworld" in the product name and select "objective-c" in the language. Other projects can input content based on their actual conditions. After setting related project options, click "Next". The subsequent steps are similar.
As shown in the figure below, a main. M file is generated in the left-side navigation pane. Open the main. M file and you can see its code in the Code interface on the right. We can also write code here.
Add swift class in the objective-C Project
We have just created an objective-C Project and need to add the swift class to the project. Right-click the helloworld group and select "New File…" from the menu ..." The create file template dialog box is displayed. As shown in, choose OS X → Source → cocoa class.
Click "Next". The page shown below is displayed. Enter "swiftobject" in the class and select swift in the language. You can leave the default value for other options.
After setting the related options, click "Next" to go to the Save file page. Select the file storage location as prompted, and then click "CREATE" to create a swift class. If there is no bridging header file in the project, xcode will also prompt whether to add the bridging header file during creation. You can choose not to add it.
Call Code
After Swift's swiftobject is created, we will see the newly added swiftobject. Swift file in the xcode project.
The SWIFT code is as follows:
import Foundation ① @objc class SwiftObject: NSObject { ② Func sayHello(greeting : String, withName name : String) ->String { ③ var string = "Hi," + namestring += greeting return string; }}
Line ① Of the above Code introduces the header file of the foundation framework. Line ② defines the swiftobject class, And the swiftobject class inherits from the nsobject class. In addition, we declare @ objc before the class, and the class declared by @ objc can be accessed by objective-C, and @ objc can also modify attributes.
The Code in line ③ defines the sayhello method, which has two parameters. The first parameter does not need to specify the external parameter name, and the second parameter (except for all parameters after the first parameter) you need to specify the external parameter name. For example, withname is the External Parameter Name of the name parameter. This is to facilitate calling in objective-C.
The following shows the objective-C code. The main. M file code is as follows:
#import <Foundation/Foundation.h>#import "HelloWorld-swift.h" ① int main(intargc, const char * argv[]) { SwiftObject *sobj = [[SwiftObjectalloc] init]; ②NSString *hello = [sobjsayHello:@"Good morning"withName:@"Tony"]; ③ NSLog(@"%@",hello); ④ return 0;}
Line ① Of the above Code introduces the header file HelloWorld-swift.h, which is required for objective-C to call swift objects and Its naming rule is "<Project Name>-Swift. H ".
Line ② instantiate the swiftobject object, and swiftobject is the class defined in swift. The Code in line ③ calls the sayhello method of swiftobject. Shows the correspondence between the methods and parameter names used in objective-C and the methods and parameters of swiftobject.
Line ④ nslog (@ "% @", hello) is used to output the result. The output result is as follows:
14:25:43. 879 helloworld [3266: 303] Hi, tonygood morning
In this way, the SWIFT code is called in objective-C. We can use this method to make full use of the existing swift files to reduce code duplication and improve work efficiency.
For more information, please refer to the first domestic swift book "swift development guide" for discussion. Website: http://www.51work6.com/swift.phpwelcome to the swifttechnology discussion group: 362298.pdf
Welcome to Zhijie IOS public classroom Platform