Using swift with cocoa and objective-c -- mix and match

Source: Internet
Author: User
Tags custom name

The compatibility between SWIFT and objective-C allows you to use both languages in the same project. You can use this "mix and match" feature to develop hybrid language-based applications. Using swfit's latest feature "mix and match", you can implement some of the functions of the application and seamlessly integrate them into the existing objective-C code.

 

Overview of mix and match Swift and objective-C files can coexist in one project, whether the project was originally based on objective-C or SWIFT. You can simply add files in another language to an existing project. This natural workflow makes it as easy to create a hybrid language application or framework target as a single language. The workflow for writing a hybrid language is only a little different, depending on whether you are writing an application or writing a framework. The following describes how to import a model into a target in two languages. More details will be provided in the subsequent sections. Code import in the same app target If you are writing a hybrid language application, you may need to use swift code to access the objective-C code, or vice versa. The process described in this chapter applies to non-framework target. Import objective-C to swiftTo import the objective-C file to the same app target for swift, You need to rely on the objective-C bridging header file (objective-C bridging header) to expose it to SWIFT. When you add swift files to an existing objective-C application, xcode automatically creates these header files, and vice versa. If you agree, xcode will generate the header file at the same time as the source file is created and name it with the-Bridging-Header.h added to the module name of the product. For details about the product Module name, see naming your product module. You should edit this header file to expose objective-C code to SWIFT. Import objective-C code to SWIFT in the same target1. In the objective-C bridging header file, import any header file you want to expose to SWIFT, for example: OBJECTIVE-C
#import "XYZCustomCell.h" #import "XYZCustomView.h" #import "XYZCustomViewController.h"
2. In build settings, make sure that the build setting of the objective-C bridge header file is based on the swfit compiler, that is, the code generation contains the path of the header file. This path must be the path of the header file rather than its directory. This path should be the relative path of 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 bridging header file will be visible to SWIFT. All swift files of the current target can use the methods in these header files without any import statements. Use these objective-C code in swift syntax, just like using the built-in class of the system. Swift
let myCell = XYZCustomCell() myCell.subtitle = "A custom cell"

 

Import swift to objective-CWhen you import Swift code to objective-C, you rely on the header file generated by xcode to expose Swift code to objective-C. This automatically generates the objective-C header file and declares all the interfaces defined in the SWIFT code in the target. You can regard this objective-C header file as the umbrella header of the SWIFT code. It is named after the product Module name and-Swift. h. For details about the product Module name, see naming your product module. You don't need to do anything to generate this header file. You just need to import it to your objective-C code to use it. Note that the SWIFT interface in this header file contains all the objc types it uses. If you use your own objective-C type in Swift code, make sure to first import the corresponding objc header file to your swift code, then, the header file automatically generated by SWIFT is imported to objc. access the SWIFT code in the M source file. Import Swift code to objective-C in the same targetIn the objc. m source file of the same target, use the following syntax to import Swift code: OBJECTIVE-C
#import “ProductModuleName-Swift.h” 

Any swift file in target will be visible to the objc. M file, including this import statement. For details about how to use swift code in objc code, see using SWIFT from objective-C.

Import code to the same framework target If you are writing a hybrid language framework, you may access the objc code from the SWIFT code, or vice versa. Import objc to swiftTo import some objc files to the SWIFT code of the target framework, you need to import these files to the umbrella header of objc for the framework to use. Import objective-C code to SWIFT in the same frameworkMake sure that the build Settings> packaging> defines module of the target framework is set to yes. Then import the objc header file you want to expose to swift access in your umbrella header file, such as: OBJECTIVE-C
#import <XYZ/XYZCustomCell.h> #import <XYZ/XYZCustomView.h> #import <XYZ/XYZCustomViewController.h> 
Swift will see all the header files exposed in the umbrella header. All swift files in the target framework can access the content of your objc file without any import statements. Swift
let myCell = XYZCustomCell() myCell.subtitle = "A custom cell" 

 

Import swift to objcTo import some swift files to the objc code of the target of the same framework, you do not need to import anything to the umbrella header file, instead, it imports the header file automatically generated by xcode for your swift code to your obj. M source file to access the SWIFT code in the objc code. Import Swift code into objective-C in the same framework1. Make sure that the defines module in build Settings> packaging of the framework target is set to yes. Use the following syntax to import Swift code to the objc. m source file under the same target framework. OBJECTIVE-C
#import <ProductName/ProductModuleName-Swift.h> 

The swift files contained in this import statement can be accessed by the objc. m source files under the same framework target. For details about how to use swift code in objc code, see using SWIFT from objective-C.

Import external framework You can import external frameworks, whether they are pure objc, pure swift, or mixed languages. The process of the import external framework is the same, whether written in one language or in two languages. When importing an external framework, make sure that the build setting> pakaging> defines module is set to yes. Use the following syntax to import the framework to swift files of different targets: swift
import FrameworkName
Use the following syntax to import the framework to the objc. M file of different targets: OBJECTIVE-C
@import FrameworkName; 

 

 

Use swift in objective-C After you import Swift code into the objc file, use the swift class in the normal objc syntax. OBJECTIVE-C
MySwiftClass *swiftObject = [[MySwiftClass alloc] init]; [swiftObject swiftMethod];
Swift classes or protocols must be marked with @ objc attribute for access in objc. This attribute tells the compiler that this swift code can be accessed from the objc code. If your swift class is a subclass of the objc class, the compiler automatically adds @ objc attribute to you. For details, see swift type compatibility. You can access the swift class or protocol to mark things with @ objc attribute, as long as it is compatible with objc. Excluding the unique features of SWIFT: generics-model tuples-tuples enumerations defined in swift-Swift the enumerated structure defined in structures defined in swift-Swift the struct top-level functions defined in swift-Swift swift defined in the top-level function global variables defined in swift-Swift defines the global variable typealiases defined in swift-Swift and defines the type alias swift-style variadics nested types-nested type curried functions-after coliization for example, a function with a fan type as a parameter, alternatively, the method for returning tuples cannot be used in objective-C. . To avoid circular references, do not import Swift code into the objective-C header file. However, you can use a swift class before declaring (Forward declare) in the objective-C header file. However, note that you cannot inherit a swift class in objective-C. Reference the swift class in the objective-C header fileSo Forward Declaration swift class: OBJECTIVE-C
// MyObjcClass.h   @class MySwiftClass;   @interface MyObjcClass : NSObject - (MySwiftClass *)returnSwiftObject; /* ... */ @end
Product Module name The name of the header file generated by xcode for swift code and the name of the objc bridge header file created by xcode are all generated from the name of your product module. By default, your product Module name is the same as the product name. However, if your product name has special characters (nonalphanumeric, non-numeric, letter characters), such as periods, they will be underlined (_) replace it with 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. xcode uses this name to name the bridging and automatically generated header files. You only need to modify the product Module name in build setting. Troubleshooting and reminder • The swift and objc files are treated as the same code set and note naming conflicts. • if you use a framework, make sure that the build setting> pakaging> defines module is set to yes; • If you use the objc bridging header file, make sure that the build setting of the objc bridging header file in build settings is based on the swfit compiler, that is, the code generation contains the path of the header file. This path must be the path of the header file rather than its directory. • Xcode uses your product Module name instead of the Target name to name the objc bridging header file and the automatically generated header file for swift. For details, see naming your product module. • To be available in objc, the swift class must be a subclass of the objc class or be marked with @ objc. • When you import swift to objc, remember that objc will not translate the unique features of swift into the corresponding features of objc. For details, see using SWIFT from objective-C; • if you use your own objc type in Swift code, make sure that the corresponding objc header file is imported to your swift code first, then, the header file automatically generated by SWIFT is imported to objc. access the SWIFT code in the M source file. This chapter is a member of the cocoachina translation team. Haolloyin ( GitHub Homepage )For Translation, please indicate the source and the translator's information, and reject commercial use.

Http://www.cocoachina.com/newbie/basic/2014/0605/8688.html

Https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_85

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.