Notes for mixed compilation of Swift and Objective-C, swiftobjective-c

Source: Internet
Author: User

Notes for mixed compilation of Swift and Objective-C, swiftobjective-c
Preface

Swift has been launched for several years. Compared with Objective-C, Swift's Language Mechanism and ease of use are more grounded, greatly reducing the entry threshold for iOS. Of course, this is not a good news for the new children's shoes, but for the whole iOS programming practitioners, it is true that, in the past few days, it was "too tall" and "Too short ". In addition, the training courses are rampant, and under Batch wholesale, iOS will no longer see the glory of that year. After the release of iOS10, the latest update is also pushed to Xcode8. Those who care about it will find that the minimum adaptation of iOS in Xcode8 has changed to iOS8.0. In addition, the Swift version tends to be stable. In a sense, the Swift era was officially launched, and it was only a matter of time to replace Objective-C. Of course, we should also be prepared before that. The more companies this year, the more they have begun to mix Swift and Objective-C.

Let's take a look at some notes and problems in the mixed compilation of the two:

Hybrid editing

There are only two mixed encoding situations,

Use Swift files in Objective-C projects or files;

Use the Objective-C file in the Swift project or file.

The most important two files in the process of mixing:

1. Bridge files

The bridge file ProjectName-Bridging-Header.h is automatically generated when another file is created for the first time. You can also add it manually if you accidentally delete it, but the name must be the ProjectName-Bridging-Header.h header file (name composition: Project name-Bridging-Header.h ), if the name is not clear, you can create a new Header file and configure the file path in Targets → Build Settings → Swift Compiler-General → Objective-C Bridging Header, this file is mainly used when Swift uses the OC class.

2. Objective-C Generated Interface Header Name file

This file is the Objective-C header file corresponding to the Swift file generated by the system during mixed encoding, you can configure the Header Name in Targets → Build Settings → Swift Compiler-General → Objective-C Generated Interface. The default file Name is the project Name-Swift. h. Generally, no changes are made.

Use Swift files in Objective-C projects or files

When the class in the Swift file is called in the OC file, the # import"

ProjectName-swift.h (name composition: Project name-swift)

This file does not exist in the project, but it actually exists. After compilation, you can press and hold Command + and click the file name to see the specific generated code.

After the introduction, you can use the specific classes directly in the OC mode.

Use the Objective-C file in a Swift project or file

When using the OC file in Swift, you only need to introduce the required header file in the bridge file, that is, the projectName-Bridging-Header.h file.

For specific use, follow the corresponding Swift syntax structure.

Notes for mixed editing

Add @ objc declaration or inherit NSObject or NSObject subclass to the Swift class to be mixed up

Class TestClass {// attributes // Implementation}

If you want to use the TestClass class in the Objective-C class, you should use @ objc to declare it, or inherit the TestClass from the NSObject or NSObject subclass. Otherwise, after the ProductName-Swift.h is introduced, the program cannot find the corresponding class.

Use third-party Framework

Set target --> build setting --> Packaging --> Defines Module to "Yes ";

Then, configure the file Target-> Build Phases-> Link Binary to add the Framework to be imported;

Finally, you need to configure the bridge file, for example, to use abc. h In the abc-lib.framework library, you need to configure it like this: # import "abc-lib/abc. h ";

Subclass

For a custom class, the Objective-C class cannot inherit from the Swift class, that is, the OC class to be compiled cannot be a subclass of the Swift class. In turn, the Swift class to be compiled can inherit from the OC class. Annotation

OC macro file

For example, to use macros defined in OC in Swift files, you can only use constants for simple macro files.

Unique Swift features

Swift has many features that OC does not have. For example, Swift has tuples, functions for first-class citizens, and exclusive enumeration types. Therefore, you must pay attention to the unique attributes of Swift when using mixed-encoding files.

Case Study Using OC block in Swift

Closure in Swift cannot use Block as the attribute to pass values. It must be an initialization method or function.
In the Objective-C file:

# Import <UIKit/UIKit. h> typedef void (^ Myblock) (NSString * arg); @ interface FirViewController: UIViewController // @ property (copy, nonatomic) Myblock myBlock; // This form is a public parameter, if callback is performed in the Swift class, there is a problem. The system prompts that there is no initialization method, so use the following method with Block as the parameter-(void) transValue :( Myblock) block; @ end

Below is the. m file

#import "FirViewController.h" @implementation FirViewController - (void)viewDidLoad {     [super viewDidLoad];     self.view.backgroundColor = [UIColor whiteColor]; } - (void)transValue:(Myblock)block{     if (block)     {         block(@"firBack");     } } @end

Callback in the Swift file:

When Swift uses the OC class, it first declares the oc header file in the bridge file.
Project name-Bridging-Header.h this is the case for creating a Swift Project

import UIKit class ViewController: UIViewController {    override func viewDidLoad()     {         super.viewDidLoad()         self.view.backgroundColor = UIColor.whiteColor()     }     @IBOutlet weak var goFirst: UIButton!     @IBAction func goFirstAction(sender: AnyObject)     {         let firVC:FirViewController = FirViewController()         firVC. transValue { ( arg:String !) -> Void in             self.aBtn?.setTitle(arg, forState: UIControlState.Normal)        }         self.navigationController?.pushViewController(firVC, animated: true) }

 

The mixed compilation of Swift and Objective-C languages is shared so much. I hope the experts can give valuable comments! (@. ε. @)

 

 

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.