Swift and objective-c mixed programming--oc call Swift

Source: Internet
Author: User

Since I'm talking about the hybrid programming of Swift and OC, my last blog, Swift and objective-c mixed programming--swift call OC, is about calling OC code in a swift project, and now we're going to implement calling Swift code in the OC project, is also very convenient. The specific implementation steps are as follows:

(1) Create an iOS project, language select OC, device select iphone.

(2) Then right-click on the project, New Files, select Cocoa Touch Class, then select Swift in the language. Inherit from NSObject. New, there will be a dialog box, select Yes.

(3) A swift class and a bridging-header.h file are now in the directory structure,


(4) Then in the new Helloswift.swift class, write a method, and then use OC to invoke.


(5) Then import the hidden header file of a project in the project's VIEWCONTROLLER.M file, in the format #import "project name-swift.h". and call Swift's SayHello method in Viewdidload (), The specific code is as follows:



(6) Run the program, the output is as follows: It is in line with our expectations.


With these implementations, we can invoke swift programs in a project based on OC Development, greatly improving our development efficiency, and we no longer have to dwell on the use of Swift or OC-based.

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

Swift and objective-c mixed programming--oc call Swift

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.