Notes for mixed Swift and Objective C

  • 2021-01-18 06:41:02
  • OfStack

Notes for Swift and Objective-C mixtures:


Swift has been launched for several years. Compared with Objective-C, Swift is more down-to-earth in terms of language mechanism and ease of use, which greatly reduces the threshold of iOS entry. Of course, this is a good news for new entrants, but for the entire iOS programming practitioners, it's true, once "big", suddenly "short". In addition, the training class is rampant, under the bulk wholesale, iOS can no longer see the glory of that year. After the launch of iOS10, followed by Xcode8 also pushed updates, careful people will find that Xcode8 iOS version minimum adaptation has become iOS8.0, plus Swift version tends to be stable, in a sense, the era of Swift officially opened, replace Objective-C afraid is only a matter of time. Of course, before that, we should also make preparations. This year, more and more companies are starting to mix Swift and Objective-C.

Let's take a look at some matters needing attention and problems in the mix of the two today:


There are only two ways to mix,

Objective-C projects or documents using Swift;

Use Objective - C files for Swift projects or documentation.

The two most important files in the process of editing:

1. Bridging files

The bridge file "ProjectName-Bridging-Header. h" is automatically generated when other files are created for the first time. If you delete it accidentally, you can add it manually, but the name must be "ProjectName-Bridging-Header.h" header file (name composition: project name -Bridging-Header.h). If you cannot remember the name, you can also create a new Header file by yourself, in Targets→Build Compiler-General →Objective-C Bridging Header configuration file path. This file is mainly used by Swift when using the OC class.

2.Objective-C Generated Interface Header Name file

Targets→Build Settings→Swift Compiler-General →Objective C Generated Interface Header Name configuration, the default file name is the project name -Swift.h, 1 generally do not change.

Objective - C projects or documents using Swift files

When calling a class from Swift in OC, you must first add #import to the OC file.

ProjectName-swift.h "(name composition: project name -swift)

This file is not visible in the project, but it does exist. After compiling, you can hold Command+ and click on the file name to see the generated code.

After the introduction, the use of specific classes can be directly used in accordance with OC.

Use Objective - C documents for Swift projects or documentation

When using the OC file in Swift, simply introduce the required header file in the bridge file, projectName-Bridging-Header.h.

For specific use, follow the corresponding Swift syntax structure.

Precautions for mixing

For Swift classes that need to be remixed, add the @objc declaration or subclass from NSObject or NSObject

 class TestClass {

//  attribute 

//  implementation 


If you want to use the TestClass class in the Objective-C class, you should declare it using @objc, or you should inherit TestClass from a subclass of NSObject or NSObject. Otherwise, the program will not find the corresponding class after introducing ProductName-Swift.h.

Use the third side Framework

Setting: target -- > build setting -- > Packaging -- > Defines Module is "Yes";

Then, the configuration file Target - > Build Phases - > Link, add the Framework to import;

abc. h: #import" abc-lib/abc.h "; abc.h: #import" abc-lib /abc.

Subclass subclass problems

For custom classes, Objective-C classes that cannot inherit from Swift, that is, the OC class to be remixed cannot be a subclass of the Swift class. Conversely, Swift classes that need to be remixed can inherit from OC classes. annotations

OC macro file

If the Swift file wants to use macros defined in OC, it can only use constant simple macros.

Swift unique features

There are many features in Swift that OC does not have. For example, Swift has tuples, functions that are citizens like 1, and unique enumeration types. Therefore, be aware of Swift unique attributes for the mixed files you want to use.

block of OC is used in the Swift case

You cannot use Block as a property to pass a value. It must be an initialization method or function.
Objective- In the C file:

#import <UIKit/UIKit.h>

typedef void (^Myblock)(NSString *arg); 

@interface FirViewController : UIViewController 
//@property (copy, nonatomic) Myblock myBlock; 
// This is the form of a public parameter Swift Class to call back, is a problem. The prompt has no initialization method, so use the following Block Is the method of the argument  

- (void)transValue:(Myblock) block;


Here is the.m file

#import "FirViewController.h" 
@implementation FirViewController 

- (void)viewDidLoad 
  [super viewDidLoad]; 
  self.view.backgroundColor = [UIColor whiteColor]; 

- (void)transValue:(Myblock)block
  if (block) 

Callback in Swift file:

When Swift uses OC classes, the header file for oc is first declared in the bridge file

Bridging-Header.h This is the case where the Swift project was created

import UIKit 
class ViewController: UIViewController 
  override func 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) 

Swift and Objective-C language mix to share so much, hope the great god give valuable opinion! (@. Epsilon. @)

Thanks for reading, hope to help you, thank you for your support on this site!

Related articles: