Mixing OC and Swift

Source: Internet
Author: User

Mixing OC and Swift

Swift's development momentum is getting better and better, and it is difficult for many companies to switch from OC to swift all at once. At this time, mixed editing is so important. I am gradually trying to mix and then gradually replace OC, so that all requests are forwarded to swift.

In fact, swift began to study the syntax and definition of swift when it first came out in 2014, but it has never been applied to practice because it is difficult for the company to change, at this time, I admire those small companies, or start-ups. Without any worries, new apps use swift... Very envious...

Okay. Let's take a look at the usage ..

First, we have an OC project called MixDemo. In this case, we need to add a swift class. Just like adding an OC class, we only select swift as the language we select.

Since my project is an OC project, when you add a swift file, Apple will ask the developer if they want to add a bridge. We need to add such a bridge.

At this time we will see an additional MixDemo-Bridging-Header.h file in our project.

We add a method to the swift file and return a string:

 

class Test4SwiftVC: NSObject {    func f1() -> String {        return "Test Mix"    }}
So the question is, how to call this method in the. m file of OC?

 

Let's first look at the build Settings in the project file:

See the red mark, the default is the project name-Swift. h, we customize as SwiftModule-Swift.h, an error occurs when modifying the project name after the side. This file is the reference header file that we call the swift class in the OC class. This class is not seen in the project, but we can click Command + to view the content of this class:

Reference this header file in the OC implementation file to call the classes in swift:

- (void)viewDidLoad {    [super viewDidLoad];    Test4SwiftVC *VC = [[Test4SwiftVC alloc] init];    [VC f1];    // Do any additional setup after loading the view, typically from a nib.}
In this way, we can call the f1 method in swift in OC.

So how to call the OC method or variable in swift?

We define a string in ViewController:

#import 
 
  @interface ViewController : UIViewController@property (strong, nonatomic) NSString *str;@end
 

Do you still remember a bridge file? It seems that we haven't used it yet. It will be useful at this time.

We add the OC header file in the MixDemo-Bridging-Header.h:

#import "ViewController.h"

Then you can reference it in the swift file...

func f2() {        var vc = ViewController()        var str: String = vc.str    }

We define the second function in swift, define a variable vc to initialize the ViewController class, and then define a String type variable to receive parameters in the OC class, in this way, you can call each other. The same is true for calling functions in OC. You can study them yourself...

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.