Objective-c Call Swift

Source: Internet
Author: User
Tags naming convention save file

Suppose you already have an old iOS app that was written using objective-c, and some of its new features need to be written in Swift, so you can call Swift from Objective-c.

Objective-c Call Swift does not require a bridge header file, but it requires Xcode generated header files. This file is generated by Xcode and does not need to be maintained by us and is not visible to developers. As seen, for example, it is able to expose classes in Swift to Objective-c, which is named: <project name >-swift.h. We need to introduce the header file into the Objective-c file, and the class in swift needs to be declared as @objc.

Create Objective-cproject

In order to better describe the mix-and-match call, we first create a objective-cproject. For the sake of simplicity, we were able to create a Mac OS x command line project.

Start Xcode 6, and then click the File→new→project menu and select "OS X→application→command line Tool" in the open choose Atemplate for your New project interface The project template (for example, as seen).


Then click on the "Next" button, which appears for example as seen in the interface.


Enter "HelloWorld" in the product name, select Objective-c in the language, and other projects can enter content in combination with their actual situation. When the relevant project options are set up, click on the "Next" button, followed by a similar step.

After creating a successful interface, for example, a main.m file is generated in the left navigation bar, as seen. Open the main.m file, you can see its code in the Code interface on the right, and we can write code here.


Join the Swift class in Objective-cproject

We have just created Objective-c project and need to join the Swift class into project. The detailed procedure is to right-select the HelloWorld group and select "New file ..." In the menu to pop up the New Document Template dialog box. For example, with what you see, select OS X→source→cocoa Class.


Then click on the "Next" button and the interface you see is displayed. Enter "Swiftobject" in class, select Swift in the language, and the other options will remain the default value.


When the relevant options are set, click on the "Next" button, enter the Save file interface, select the location where the file is stored, and click the "Create" button to create the Swift class. If you do not have a bridge header file in project, Xcode will also prompt us to join the bridge header file during the creation process, so we can choose not to join.

Calling code

After the Swift Swiftobject is created, we will see the newly added Swiftobject.swift file in the Xcodeproject.

Swiftobject.swift code such as the following:

Import Foundation                                                               ① @objc class Swiftobject:nsobject {                     ②func SayHello (greeting:string, Withname name:string) ->string {      ③var string = "Hi," + namestring + = greeting return string;}    }

The preceding code, line ①, introduces the header file of the foundation framework. The ② code defines the Swiftobject class, and the Swiftobject class inherits from the NSObject class. In addition, we declare in front of the class as @objc, @objc declared class can be objective-c access, @objc can also modify properties.

The ③ code defines the SayHello method, which has two parameters, the first parameter does not need to specify an external parameter name, and the second parameter (except for the first full number of references) needs to specify an external parameter name, such as the external parameter name of the name parameter for Withname. This is for ease of invocation in objective-c.

The following is a look at the objective-c-side code, MAIN.M file code such as the following:

#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;}

The preceding code, ①, introduces the header file Helloworld-swift.h, which is required for objective-c to invoke the Swift object, and its naming convention is "<project name >-swift.h".

The ② Line code instantiates the Swiftobject object, and Swiftobject is the class defined in Swift. The ③ line code calls Swiftobject's SayHello method, which is called in Objective-c when the method and the parameter are named with the corresponding relationship between the Swiftobject method and the parameters, as seen in the.


The ④ Line code nslog (@ "%@", hello) is used to output the results, such as the following:

2014-07-05 14:25:43.879helloworld[3266:303] Hi,tonygood morning

This makes it possible to invoke Swift code in Objective-c, which allows us to take advantage of the existing swift files and reduce the ability to write code repeatedly and work more efficiently.


A lot of other content please pay attention to the first swift book "Swift Development Guide" book Exchange Discussion site: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485

Welcome to Luxgen iOS Classroom public Platform



Objective-c Call Swift

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.