Analysis on Mixed Development of Object-C and Swift

Source: Internet
Author: User

Analysis on Mixed Development of Object-C and Swift

Object-C has served Apple's iOS App language for many years. In 2014, Apple launched the new programming language Swift, the more efficient and safer slogan once again attracted a large number of non-iOS developers to enter. Xiao Bian believes that replacing Object-C with Swift is only a matter of time. In the land of China, after all, technology popularization lags behind, however, in about two years, the Swift product chain will be formed, and the third-party support framework will be gradually improved. Swift will surely become the mainstream iOS development. Currently, the status quo of radical deployment is Swift and Object-C hybrid development.

Call Swift in Object-C

Next we will share with you how to use Swift programming in the Object-C Project.

First create an Object-C Project

Right-click the project name and click "New File…" in the shortcut menu ..." Menu items, as shown in. In the create file dialog box, click the "Cocoa Touch Class" list item, and then click "Next" to go to the Next step.


3. enter the Class Name of the new class, select the desired parent class type, switch the programming language to Swift, and click "Next". The dialog box shown in is displayed, the main prompt is whether to automatically generate a hidden Street Bridge file. Obviously, you need to Create the file here. Click Create Bridging Header to Create a Swift class in Object-C.

Write a method in Swift to test it: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> func SayHi(){print("Hello,Welcome to call me!")}

4. When calling Object-C, you must note that you need to introduce hidden header files in the class.

# Import "project name-swift. h"

Specifically, here is:

 #import “BlendSO-swift.h”

5. Finally, you can safely call the Swift class.

SwiftClass *swift=[[SwiftClass alloc]init];[swift SayHi];

How to call Object-C in the Swift project? In fact, it is similar to the above

Call Object-C in Swift

First create a Swift Project

Right-click the project name and click "New File…" in the shortcut menu ..." Menu items, as shown in. In the create file dialog box, click the "Cocoa Touch Class" list item, and then click "Next" to go to the Next step.

3. enter the Class Name of the new class, select the desired parent class type, switch the programming language to Objective-C, and click "Next". The dialog box shown in is displayed, the main prompt is whether to automatically generate an explicit Bridge Street file. Obviously, you need to Create it here. Click Create Bridging Header so that an Objective class is created in Swift.

Test in the newly created Objective-C:

ObjectiveCClase. @ interface ObjectiveCClase: NSObject @ property (nonatomic, copy) NSString * name;-(id) initWithName :( NSString *) name;-(void) SayHi :( NSString *) name; @ end ObjectiveCClase. m-(id) initWithName :( NSString *) name {self = [super init]; self. name = name; return self;}-(void) SayHi :( NSString *) name {NSLog (@ "[email protected]: Swift, please call me! ", Name );}

4. the difference here is that the Class header file name in Objective-C needs to be added to the explicit bridging header file, but not in Objective-C (it cannot be called because it is implicit)

5. Finally, call the Objective-C class in Swift to implement the functions in OC.

    let objective=ObjectiveCClase(name: "Objective-C")    objective.SayHi("Objective-C")
Summary:

The interaction steps between Swift and Objective-C are basically the same in the early stage.

In the middle stage, you will be prompted to generate a bridge file. The first time you create a bridge file, you will not be prompted again, but there is a difference between the two: in Objective-C, Swift is generated when Swift is called, While Swift is an explicit bridge file when Objective-C is called.

Finally, when calling Swift in Objective, add the format # import "project name-swift. h "header file, but when Swift calls Objective-C, it introduces the Objective-C header file in the explicit bridge file.

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.