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