OC and Swift call each other

Source: Internet
Author: User
Tags uikit

OC and Swift call one, OC invoke Swift file two, Swift call OC file Three, note and summarize add: Iv. custom bridging file One, OC invoke 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.import the header files needed to use OC in the bridging file#import "Sec.h"  3.compiling, you can invoke the method of the OC file in swift with Swift syntax.Let second = sec ()Second.actionmake ()

iii. attention and summary

Note:1. OC Project before you import swift files, create a bridging file as prompted by the system.2. Set the defines Module to Yes in packaging, targets, Build Settings3. When invoking swift files in OC, classes in swift inherit NSObjectOtherwise, there's no way to call it in OC4. Call the Swift file in OC to import "project name-swift.h"(This is the name of build Setting-packaging->product Module name, and this property is the project name by default)5, finished writing remember command + B compile6, Swift Project reference OC file also need to create a bridge file, in this bridge file import OC file header file7, bridge file only one, function only for swift reference OC file, OC Reference Swift file Although also need to create bridge fileHowever, you do not need to import swift files in the bridging file8, if a project both OC Reference Swift also has swift reference OC, first set OC call Swift filein 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.