A hybrid programming approach that uses both Swift and OBJECTIVE-C code in a project _swift

Source: Internet
Author: User
Tags mixed custom name

Swift's compatibility with objective-c allows you to use both languages simultaneously in the same project. You can use this feature called mix and match to develop a hybrid-language application that can use the latest features of Swfit to implement a subset of the functionality of the application and seamlessly incorporate 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 source files for another language directly to the existing project. This natural workflow makes it easy to create a mixed-language application or framework target as simple as using a single language.

The workflow for mixed languages is only a little different, depending on whether you're writing an application or writing a framework. The following describes the common case for importing a model in one target in two languages, with more details in subsequent chapters.

Import in target of the same application

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

Import Objective-c to Swift

When importing some objective-c files into an application target for swift code use, you need to rely on the Objective-c Bridge header file (bridging header) to expose to Swift. When you add Swift files to an existing OBJECTIVE-C application (or vice versa), Xcode automatically creates the header files.

If you agree, Xcode will generate the header file at the same time as the source file creation and name it with the product's module name plus-bridging-header.h. The module name for product is detailed in naming Your product module.

You should edit this header file to expose 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 headers you want to expose to Swift, for example:

Copy Code code as follows:

Objective-c

#import "XYZCustomCell.h"
#import "XYZCustomView.h"
#import "XYZCustomViewController.h"

2) Make sure that the build setting of the Objective-c Bridge header file is based on the Swfit compiler, the Code Generation the path that 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 of your project, similar to the path specified by Info.plist in the build Settings. In most cases, you do not need to modify this setting.

All public objective-c headers listed in this bridge header file will be visible to Swift. All Swift files for the current target can then use the methods in these header files and do not need any import statements. Use these objective-c codes in swift syntax just as you would with the Swift class that the system brings with it.

Copy Code code as follows:

SWIFT

Let MyCell = Xyzcustomcell ()
Mycell.subtitle = "A Custom Cell"

Import Swift into Objective-c

When you import swift code into OBJECTIVE-C, you rely on Xcode generated header files to expose the swift code to objective-c. This is the automatic generation of the OBJECTIVE-C header file, which contains the interfaces defined in all Swift codes 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. The module name for product is detailed in naming Your product module.

You don't have 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 the SWIFT code, make sure that you import the corresponding objective-c header into your swift code before you import the automatically generated header file from Swift 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:

Copy Code code as follows:

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

Any Swift files in target will be visible to the objective-c. m source file, including this import statement. For more information on using the SWIFT code in objective-c code, see the Using swift from Objective-c.

Import to Swift import to Swift
Swift Code Import statement not 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 hybrid language framework, you may access 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 for the frame target is set to Yes. Then, in your umbrella header file, import the objective-c header file you want to expose to Swift access, for example:

Copy Code code as follows:

Objective-c
#import <XYZ/XYZCustomCell.h>
#import <XYZ/XYZCustomView.h>
#import <XYZ/XYZCustomViewController.h>

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

Copy Code code as follows:

SWIFT
Let MyCell = Xyzcustomcell ()
Mycell.subtitle = "A Custom Cell"

Import Swift into Objective-c

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

Import Swift code into OBJECTIVE-C in the same framework

Make sure that the defines Module in the build Settings > Packaging of the frame 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.

Copy Code code as follows:

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

The Swift file contained in this import statement can be accessed by the objective-c. m source file under the same framework target. For more information on using the SWIFT code in objective-c code, see the Using swift from Objective-c.

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

Use Swift in Objective-c

After you import the SWIFT code into the Objective-c file, use the Swift class with the usual OBJECTIVE-C syntax.

Copy Code code as follows:

Objective-c

Myswiftclass *swiftobject = [[Myswiftclass alloc] init];
[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 the Swift code can be accessed from objective-c code. If your Swift class is a subclass of the Objective-c class, the compiler will automatically add @Objective-C attribute to you. See Swift Type compatibility for details.

You can visit the Swift class or protocol with the @Objective-C attribute marked as long as it is compatible with objective-c. Does not include the unique features of Swift:

generics-Fan type
tuples-tuples
Enumeration defined in the enumerations defined in Swift-swift
structures The structure defined in the defined in Swift-swift
Top-level functions defined in the top-level functions defined in Swift-swift Swift
Global variables defined in the global variables defined in Swift-swift
typealiases the type alias defined in the defined in Swift-swift
swift-style variadics-swift Style variable parameters
Nested Types-nested type
function of curried functions-Cauchy

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

To avoid circular references, do not import Swift code into the objective-c header file. But you can use it in front of the Objective-c header file (forward declare) as 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

So forward declaration Swift class:

Copy Code code as follows:

Objective-c
Myobjective-cclass.h

@class Myswiftclass;

@interface Myobjective-cclass:nsobject
-(Myswiftclass *) Returnswiftobject;
/* ... */
@end

Product module naming

The name of the header file that Xcode generated for the Swift code, and the name of the Objective-c Bridge header file that Xcode created, are generated from your product module name. Your product module name is the same as the product name by default. However, if your product name has special characters (Nonalphanumeric, non-numeric, alphabetic characters), such as dot numbers, they will be replaced with 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 give the product module name a custom name that Xcode to name the bridged and auto-generated header files. You only need to modify the Product Module Name in Build setting.

Problem Solving Tips

• View Swift and objective-c files as the same Code collection and note naming conflicts;
• If you use the framework, make sure that 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 is based on the Swfit compiler, which is the path of the Code Generation containing the header file. The path must be the path to the header file itself, not the directory in which it resides, and
Xcode uses your product module name instead of the target name to name the Objective-c Bridge header file and the header file automatically generated 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 @Objective-c tag;
• When you will be swift to guide When entering into Objective-c, remember that objective-c does not translate Swift's unique features into objective-c corresponding features. See the list using Swift from OBJECTIVE-C
• If you use your own objective-c type in the SWIFT code, be sure to import the corresponding Objective-c header file into your swift code before you Swi FT automatically generated header files import into the objective-c. m source file to access the Swift code.

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.