Swift calls objective-C

Source: Internet
Author: User

Swift calls objective-C requires a bridging header file named <Project Name>-Bridging-Header.h, as shown in. The purpose of bridging the header file is to build a bridge for swift to call the objective-C object, which must be named "<Project Name>-Bridging-Header.h ", we need to introduce the objective-C header file in the bridging header file, and the bridging header file needs to be managed and maintained.
 

Create a swift Project
To better introduce mixed call, we first create a SWIFT project. For simple consideration, we can create a Mac OS X command line project instead of an iOS project.
Start xcode 6, click the file → new → project menu, and select the "OS X → application → command line tool" project template on the open choose a template for your new project interface.
Click "Next". The page shown in is displayed.
We can follow the prompts and enter relevant content based on our actual situation and needs. The following briefly describes the engineering options in 18-3.
Product Name: Project name.
Organization Name: name of the organization.
Organization identifier: organization identifier (important ). Generally, the organization name or company domain name (such as net. cocoagame) is entered here, which is similar to the package name in Java.
Bundle identifier: bundle identifier (important ). The identifier consists of product name + organization identifier. Because it is used when publishing an application on the App Store, its name cannot be repeated.
Language: select the development language. Here we can select the language used for application development, and in xcode 6 we can select swift and objective-C.
After setting related project options, click "Next" to go to the next page. Select the location where the file is stored as prompted, and click "CREATE" to display the interface shown in.
Open the main. Swift file in the navigation bar on the left, and you will see the code interface shown in. We can compile Swift code here.
Add the objective-C class to the SWIFT project
We have just created a SWIFT project. We also need to call other objective-C classes to implement some functions. We need to add the objective-C class to the SWIFT project. Right-click the helloworld group and select "New File…" from the menu ..." The create file template dialog box is displayed, as shown in. select OS X → Source → cocoa class ".
Click "Next". The page shown in is displayed. Enter "objcobject" in the class and select "objective-c" in the language. Keep the default values for other options.
After setting related options, click "Next" to go to the Save file page, select the location where the file is stored as prompted, and then click "CREATE" to create the objective-C class. If the objective-C class is created for the first time, xcode will prompt you whether to add the bridging header file after saving the file, as shown in. In the displayed dialog box, select "yes ", the bridge connector file HelloWorld-Bridging-Header.h is created in the project.
Call Code
After objective-C's objcobject is created, we will see the two newly added files objcobject. h and objcobject. m in the xcode project. This book does not describe objective-C language too much, but to better understand the mutual calls between SWIFT and objective-C, we will simply explain objective-C code.
The objcobject. H code is as follows:
# Import <Foundation/Foundation. h> ①


@ Interface objcobject: nsobject ②


-(Nsstring *) sayhello :( nsstring *) greeting withname: (nsstring *) Name; ③


@ End
The objcobject. h file is the header file of objective-C. Here we define the class and declare the member variables and methods of the class. Line ① introduces the header file of the foundation framework. The Code definition class objcobject in line ② inherits from the nsobject parent class. The nsobject class is the root class of all objective-C. The Code in line ③ declares the instance method sayhello: withname:, which has two parameters: greeting and name.
The objcobject. m code is as follows:
# Import "objcobject. H" ①


@ Implementation objcobject


-(Nsstring *) sayhello :( nsstring *) greeting withname: (nsstring *) Name ②
{
Nsstring * string = [nsstringstringwithformat: @ "Hi, % @.", name, greeting];
Return string;
}


@ End
Line ① Of the above Code introduces the objcobject. h header file, and line ② defines sayhello: withname: method.
Next let's take a look at the code for calling the main. Swift file in swift:
Import Foundation ①


Varobj: objcobject = objcobject () ②
VaR Hello = obj. sayhello ("good morning", withname: "Tony") ③


Println (Hello) ④
The import Foundation statement in line ① Of the above Code introduces the foundation framework, similar to the # import <Foundation/Foundation. h> statement of objective-C. We will introduce the foundation framework in the next chapter.
The Code in line ② declares and instantiates the object of the objcobject class. Objcobject is the objcobject defined in objective-C.
Line ③ calls the sayhello: withname: Method of the objcobject class. Note that the method name and parameter used for swift calling correspond to the method name and parameter in objective-C, as shown in.
 
Line ④ prints the output result. The output result is as follows:
Hi, Tony good morning.
To enable swift to call objective-C, you must also introduce the objective-C header file in the bridging header file. Helloworld-Bridging-Header.h code is as follows:
# Import "objcobject. H"
Very simple: there is only one line of code. If there are other objective-C header files, you need to introduce them here.
In this way, the objective-C code is called in swift. We can make full use of the existing objective-C file to reduce code duplication and provide work efficiency.
So far, the entire project has been created. As shown in 18-10, click "run" in the upper left corner to view the running result.
 




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






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.