Swift Object-c calls each other

Source: Internet
Author: User

Let's take a look at the swift call Object-c

First we create a OC class

At the last step, I found a confirmation box, click Yes the system will generate a conversion bridge for Swift and OC.

His naming rules are the project name-bridging-header.h This is the system generated by our direct use.

Next we add a method to the OC class that we just created

#import <Foundation/Foundation.h> @interface testswift2oc:nsobject-(void) testocstr; @end


Implementation method


#import "TestSwift2OC.h" @implementation testswift2oc-(void) testocstr{    NSLog (@ "This is the OC Method");} @end


Then we add a statement to the system-generated file testswiftoc-bridging-header.h.

#import "TestSwift2OC.h"


Well, we can call it in swift, try it.

        var testswift2oc=testswift2oc ()        testswift2oc.testocstr ()


Did it come out already?


Now let's take a look at the OC call Swift

We create a new swift to define a method

Import Uikitclass testoc2swft:nsobject {    func testswftstr () {        println ("This is the Swift method")    }}

Then we first import a header file in the OC class that needs to invoke the Swift class

Project Name-swift.h (this is automatically generated by the system)

Look at the code

#import "Testswiftoc-swift.h"

Then we call in OC

    TESTOC2SWFT *testoc2swift=[[testoc2swft Alloc]init];    [Testoc2swift TESTSWFTSTR];

The call is ready.




Apple Development Group: 414319235 Welcome to join the Welcome discussion question


Apple Development Group: 414319235 Welcome to join the Welcome discussion question

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift Object-c calls 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.