OC and Swift call each other

Source: Internet
Author: User
Tags uikit

First, the OC calls the swift file two, the SWIFT calls the OC file three, the attention and the summary adds: Four, the custom bridge file one, OC calls Swift file

When you create a swift file in the OC project, Xcode prompts you to create a bridging file, click OK to create the bridging file, and Xcode will automatically create a bridging file

Name: Project Name-bridging-header.h

This bridging file is used by Swift when calling OC files.

1.

Enter targets->build Settings, packaging

Set defines module to Yes

Set the Product Module name, or do not set the name of the project by default. This is going to be used in the back.

2.

Write a class in Swift

Note that this class must inherit nsobject, otherwise it can't be used in OC

Class Hello:nsobject {
var address:string
var gender:string
Init (address:string,gender:string) {self.address = Address
Self.gender = Gender
}
Func method () {print ("message \ (self.address + self.gender)")}}3,

Import the file "product module Name-swift.h" in the Swift file that OC needs to use because the product module name is the project name by default

So direct import #import "project name-swift.h"

4.

Remember to write the command +b compile, if there is no error can be in the OC file to invoke the class in Swift

Hello *helloss = [helloalloc]initwithaddress:@ "Shanghai" gender:@ "male"]; [Helloss Method];

Second, Swift calls OC file

When you create an OC file in a swift project, Xcode prompts you to create a bridging file point to make sure that the bridging file is created, and Xcode automatically creates a bridging file

Name: Project Name-bridging-header.h

Put the header file of the OC file that Swift needs to use in the bridge file. Project name-bridging-header.h

For example:

1, this is an OC file//#import "sec.h" #import <UIKit/UIKit.h> @interface Sec:uiviewcontroller
-(void) actionmake; @end 2, in the bridge file in the need to use the OC header file into the #import "sec.h" 3, compile, you can use swift syntax in Swift to call OC file method of Let Second = sec () Second.actionmake ()

Iii. Attention and summary

Note: 1. OC Project Create a bridging file as prompted before importing the Swift file, 2, set defines Module to YES3 in Build Settings-targets, packaging, call Swift in OC , the class in Swift will inherit nsobject otherwise, there is no way to call the Swift file in OC 4, OC to import "project name-swift.h" (This is build Setting, Packaging->product Mo Dule name, and this property is the name of the project by default) 5, after writing the COMMAND + B compile 6, the SWIFT Project reference OC file also need to create a bridge file, in this bridge file import OC file header file 7, bridge files only one, the role is only for Swift Reference OC file, OC Reference Swift File Although you also need to create a bridge file but do not need to import the Swift file in the bridge file 8, if a project both OC Reference Swift also has swift reference OC, first set OC call Swift file in the bridge file import OC File, OC can define PCH file import "project name-swift.h"

Iv. Custom Bridging files

You can also customize a bridging file if you do not need to create a bridging file without the Xcode hint

1 use Header File to empty the contents.

2, this file name can be customized, generally or with the project name-bridging-header.h such as App-file.h

3 then in Build Setting, Swift compiler-code generation

4, set objective-c bridging Header for the custom bridge file relative to the absolute path of the project, that is, the path of the total folder, if the first layer, directly write the custom bridge file name File.h, if the next layer, Folder/bridge file app/ File.h

OC and Swift call each other

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.