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