Simultaneous use of swift and objective-c in one project

Source: Internet
Author: User
Tags custom name

Swift's compatibility with objective-c allows you to use both languages in the same project. You can use this feature called mix and match to develop a mixed-language application that can implement part of the app's functionality with the latest features of Swfit and seamlessly incorporate into existing objective-c code.

Mix and Match Overview

Objective-c and Swift files can coexist in a project, whether the project was originally based on objective-c or Swift. You can simply add a source file for another language directly to an existing project. This natural workflow makes it easy to create a mixed-language application or framework target, as in a single language.

The workflow of a mixed language is only a little bit different, depending on whether you are writing the application or writing the framework. The following describes the normal use of two languages in a target to import the model, the subsequent chapters will have more details.

Import in target of the same app

If you are writing a mixed-language application, you may need to access the OBJECTIVE-C code with Swift code, or vice versa. The following procedure describes the application in non-frame target.

Import Objective-c to Swift

When importing some objective-c files for use by Swift code in an app's target, you need to rely on the bridge header file (bridging header) with objective-c to expose to Swift. When you add Swift files to an existing Objective-c app (or vice versa), Xcode automatically creates the header files.


If you agree, Xcode generates a header file at the same time that the source file was created and is named with the product's module name plus-bridging-header.h. For the module name of product, see Naming Your product module.

You should edit this header file to expose the Objective-c code to Swift.

Import objective-c code into Swift in the same target

1) in the Objective-c Bridge header file, import any header files you want to expose to Swift, for example:

    1. Objective-c
    2. #import "XYZCustomCell.h"
    3. #import "XYZCustomView.h"
    4. #import "XYZCustomViewController.h"


2) Make sure that the build setting of the Objective-c Bridge header file in Build Settings is based on the Swfit compiler, which is the path where the Code Generation contains the header file. This path must be the path of the header file itself, not the directory in which it resides.

This path should be the relative path to your project, similar to the path specified by Info.plist in Build Settings. In most cases, you do not need to modify this setting.

All public objective-c header files listed in this bridge connector file will be visible to Swift. All Swift files for the current target can then use the methods in these header files without any import statements. Use the OBJECTIVE-C code in swift syntax just as you would with a swift class that comes with your system.

    1. SWIFT
    2. Let MyCell = Xyzcustomcell ()
    3. Mycell.subtitle = "A Custom Cell"
Import Swift into the Objective-c

When you import swift code into OBJECTIVE-C, you rely on the header files generated by Xcode to expose the swift code to objective-c. This is an auto-generated objective-c header file that contains the interfaces defined in all Swift code in your target. You can think of this objective-c header file as the umbrella header of the Swift code. It is named with the product module name plus-swift.h. For the module name of product, see Naming Your product module.

You don't need to do anything to generate this header file, just import it into your objective-c code to use it. Note that the Swift interface in this header file contains all the objective-c types that it uses. If you use your own objective-c type in swift code, be sure to import the corresponding Objective-c header file into your swift code before importing the Swift Auto-generated header file into the objective-c. m source file to access S Wift code.

Import Swift code into objective-c in the same target

In the objective-c. M source file of the same target, use the following syntax to import the SWIFT code:

    1. Objective-c
    2. #import "Productmodulename-swift.h"

Any Swift file in target will be visible to the objective-c. m source file, including this import statement. For the use of Swift code in OBJECTIVE-C code, see Using Swift from Objective-c.

to Swift
Import
Swift Code No import statement required #import
Objective-c Code No import statement required; Objective-c ' umbrella header file required #import "Header.h"
Import in target of the same Framework

If you are writing a mixed language framework, you may be able to access the OBJECTIVE-C code from the Swift code, or vice versa.

Import Objective-c to Swift

To import some objective-c files into the Swift code of the same frame target, you need to import these files into the Objective-c umbrella header for the framework to use.

Import objective-c code into Swift in the same framework

Make sure that the Build Settings > Packaging > Defines Module of the framework target is set to Yes. Then import the objective-c header file that you want to expose to Swift access in your umbrella header file, for example:

    1. Objective-c
    2. #import <XYZ/XYZCustomCell.h>
    3. #import <XYZ/XYZCustomView.h>
    4. #import <XYZ/XYZCustomViewController.h>

Swift will see all the header files you expose in the umbrella header, and all swift files in the framework target can access the contents of your objective-c file without any import statements.

    1. SWIFT
    2. Let MyCell = Xyzcustomcell ()
    3. Mycell.subtitle = "A Custom Cell"
Import Swift into the Objective-c

To import some swift files into the objective-c code of the target of the same framework, you do not need to import anything into the umbrella header file, but instead import Xcode into your Obj. M source for your swift code auto-generated header file. File to access the Swift code in the OBJECTIVE-C code.

Import Swift code into OBJECTIVE-C in the same framework

Make sure that the defines Module in Build Settings > Packaging of the framework target is set to Yes. Use the following syntax to import the Swift code into the objective-c. m source file under the same framework target.

    1. Objective-c
    2. #import <ProductName/ProductModuleName-Swift.h>

The import statement contains Swift files that can be accessed by the objective-c. m source file under the same framework target. For the use of Swift code in OBJECTIVE-C code, see Using Swift from Objective-c.

to Swift
Import
Swift Code No import statement required #import
Objective-c Code No import statement required; Objective-c ' umbrella header file required #import "Header.h"
Import External Framework

You can import an external framework, whether it's pure objective-c, pure Swift, or mixed language. The process of import external frames is the same, whether the framework is written in one language or two languages. When you import an external frame, make sure that Build Setting > pakaging > Defines Module is set to Yes.

Use the following syntax to import the framework into Swift files of different target:

    1. SWIFT
    2. Import FrameworkName

Use the following syntax to import the framework into the objective-c. m file for different target:

    1. Objective-c
    2. @import frameworkname;
Import into Swift import to Objective-c
Any language frame Import FrameworkName @import frameworkname;
Using Swift in Objective-c

After you import the SWIFT code into the Objective-c file, use the Swift class with the normal objective-c syntax.

    1. Objective-c
    2. Myswiftclass *swiftobject = [[Myswiftclass alloc] init];
    3. [Swiftobject Swiftmethod];

Swift's class or protocol must be marked with @Objective-C attribute to be accessible in objective-c. This attribute tells the compiler that this Swift code can be accessed from the OBJECTIVE-C code. If your Swift class is a subclass of the Objective-c class, the compiler will automatically add @Objective-C attribute for you. See Swift Type compatibility.

You can access the Swift class or protocol with @Objective-C attribute tag, as long as it is compatible with objective-c. Excluding these features that are unique to Swift:

    • Generics-Fan Type
    • Tuples-tuples
    • Enumerations the enumeration defined in the defined in Swift-swift
    • Structures the structure defined in the defined in Swift-swift
    • Top-level functions defined in Swift-swift the top-level function defined in Swift
    • Global variables defined in the variables defined in Swift-swift
    • Typealiases type aliases defined in the defined in Swift-swift
    • Swift-style variadics-swift Style variable parameter
    • Nested types-Nested types
    • Curried functions-The function after the Cauchy


For example, a method with a generic type as a parameter or a return tuple cannot be used in objective-c.

To avoid circular references, do not import the Swift code into the objective-c header file. However, you can use it in the OBJECTIVE-C header file before declaring (forward declare) a swift class, but note that you cannot inherit a swift class in Objective-c.

Referencing the Swift class in the Objective-c header file

This way forward declares the Swift class:

    1. Objective-c
    2. Myobjective-cclass.h
    3. @class Myswiftclass;
    4. @interface Myobjective-cclass:nsobject
    5. -(Myswiftclass *) Returnswiftobject;
    6. /* ... */
    7. @end
Product module naming

The name of the header file generated by Xcode for Swift code, and the name of the Objective-c Bridge Connector file created by Xcode, are generated from your product module name. By default, your product module name is the same as the product name. However, if your product name has special characters (nonalphanumeric, non-numeric, alphabetic characters), such as the dot, then they will be replaced by an underscore (_) as your product module name. If the product name starts with a number, the first number is replaced with an underscore.

You can provide a custom name for the product module name that Xcode uses to name the bridged and automatically generated header files. You only need to modify the Product Module Name in the build setting.

Problem Resolution Tips
      • Think of Swift and objective-c files as the same code set, with attention to naming conflicts;
      • If you use frames, make sure the Build Setting > Pakaging > Defines Module is set to Yes;
      • If you use the Objective-c Bridge header file, make sure that the build setting of the Objective-c Bridge header file in Build Settings is based on the Swfit compiler, which is the path where the Code Generation contains the header file. This path must be the path of the header file itself, not the directory in which it resides;
      • Xcode uses your product module name instead of the target name to name the Objective-c Bridge header file and the auto-generated header file for Swift. See naming Your Product Module;
      • In order to be available in Objective-c, the Swift class must be a subclass of the Objective-c class or be tagged with @Objective-c;
      • When you import swift into Objective-c, remember that OBJECTIVE-C does not translate features that are unique to swift into OBJECTIVE-C counterparts. See list using Swift from Objective-c;
      • If you use your own objective-c type in swift code, be sure to import the corresponding Objective-c header file into your swift code before you import the Swift Auto-generated header file to objective-c. m source File To access the Swift code.

Simultaneous use of swift and objective-c in one project

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.