Swift and objective-c mixed programming--swift call OC

Source: Internet
Author: User

In the development of iOS apps. Swift will replace OC, the trend is "short-term coexistence, long-term substitution." But there was too much code that was finished with OC language, and Swift's development also inherited a lot of features from OC. There are also a lot of similarities between the two.

Swift and OC are now able to seamlessly mix programming.

I will implement a simple swift call OC iOS instance, the implementation process is very simple.

Just like all of our Hello world.

Detailed operating procedures such as the following:

(1) Create a new iOS project in Xcode and select Swift in the language. Device select iphone. folder structure such as the following:


(2) Then in the project right-click New File, select Cocoa Touch class, enter the class name, inherit NSObject, language selection objective-c. Select Next. The first time you create an OC class in Swift, you will get a popup such as the following dialog box, click Yes to:


(3) After the OC class is created, the folder structure of the project, such as the following: Three files found in the folder structure, HELLO.H,HELLO.M are each OC Class I created, and so on will be called with Swift. The other is bridging-header.h, which is able to interpret this as a bridging file between OC and Swift.


(4) Then in Bridging-header.h, introduced the OC class header file, the code is: #import "Hello.h"

(5) Then implement a simple method in the OC class. Output a statement that is declared in Hello.h first:

The method is then implemented using the OC syntax in HELLO.M:


(6) Then the method of calling OC in the Viewdidload () of Swift's class Viewcontroller.swift:


(7) The final print results are executed,

.


At this point, Swift can easily invoke the OC code.

is not very convenient.


GitHub home: https://github.com/chenyufeng1991. Welcome to visit us!

Swift and objective-c mixed programming--swift call OC

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.