Cross-Use of swift and OC in projects

Source: Internet
Author: User
Swift and objective-C in the same project
Cross-Use of swift and OC in projects

The compatibility between SWIFT and OC allows you to use swift + oC to write applications in the project, called mix and match ), in this way, you can enjoy the fast features of the new language while seamlessly joining your existing OC Code (the original text sounds like Nb, which is actually the case ), whether the project was originally written with OC or a new SWIFT project, you can continue development with OC and swift in this project.



Importing objective-C into swift

When you add an OC file to the swfit code, you need to rely on a bridging header file. xcode will automatically create an OC file when you create it (the project is created using the swift language ), click Yes, xocde will help us create a new xxx-Bridging-Header.h file while creating the OC file, XXX is your project name (product module ), then we have to edit this file so that the swfit code can call the OC code.


In the project's xxx-Bridging-Header.h file, add the OC file that needs to be called in swift, and then you can call it in swfit code

OBJECTIVE-C
#import "XYZCustomCell.h"#import "XYZCustomView.h"#import "XYZCustomViewController.h"

Swift
let myCell = XYZCustomCell()myCell.subtitle = "A custom cell"

In the build settings option of the project, make sure that the setting of the bridging header file exists in the swift compiler option. The path must point to the file itself, not the directory.


Importing swift into objective-C

When swift code is called in OC, a header file automatically generated by xcode is required. This automatically generated header file defines all the swift files in the project, this header file named "xxx-Swift.h", XXX is the name of the project, in addition to no need to do anything, you just need to import such a file in the OC code, you can use any Swift code just like you can.
#import “ProductModuleName-Swift.h”

The preceding table is summarized as a table.

  Call Swift code Call OC code
Swift code No need to declare # Import "ProductModuleName-Swift.h"
OC code Depends on the bridging header file # Import "header. H"



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.