The company has officially used SWIFT language to develop projects for more than a year, now summarize the experience of SWIFT and OC calling each other

Swift call OC

  • Swift calls OC class, must have a bridge file, and import OC class. H file in the bridge file.
1. Create"Project name" -bridge-header. hfile
  • Creating a new project will automatically prompt you to create the Bridging file SwiftTestProject(project name) -bridge-header. h, which is required for swift to call OC.

Create the bridge file

  • If the -bridge-header. h File is not selected for a new project, you need to manually create a File named “project name” -bridge-header. h and create a Header File named “project name -bridge-header”. Content to empty

  • Then in the project set :TARGET -> Build Settings -> Objective-c Bridging Header Fill in your file name: “project name” -bridging – header.h

  • Under the TARGETS > Build Settings area, set this parameter to YES

2. The.h header of OC can be used to call the relative OC class in swift project.

Oc call swift

1. Engineering configuration

TARGETS –>Build Settings search for Product Module Name as the project Name, then the project will automatically create a “project Name” -swift. h file. Swifttestproject-swift. h is used by oc to call Swift files. This file is not displayed by default.

2. The introduction of"Project name "- swift.hfile

#import “project name -swift. h” in the OC file, note that this file does not prompt, manually type, then use the Swift class in the OC class.

Attention and summary

  • Oc references swift file to reference the project name-swift. h file, which needs to be manually typed. If no prompt is displayed, swift references oc file to create a bridge file, which references the. H file of oc

  • Notice that OC calls swift, swfit,

Pay special attention toOC calls the Class Class in Swift

  1. This class must be inheritedNSObjecorNSObjecA subclass of
  2. Class@objcMembersThis keyword is used for all properties and methods of the class@objc), before 3.0 is apple automatically help you add, it is said that they dislike packaging will be a little larger, later changed to their own manual add. Such as:
@objcMembers class CustomServiceTool: NSObject {}

Copy the code
  1. Class calls to protocols, enumerations, structs, things like that
  • Protocol: Just add one in front@objc
@objc protocol CustomServiceToolDelegate: NSObjectProtocol {}

Copy the code
  • Enumeration: must be definedIntType, preceded by@objc(After all, swift’s enumeration is much more powerful than OC’s)
@objc enum CPLineType: Int {
case schoolToHome = 0
case homeToSchool
case return
}

Copy the code
  • Struct (@objc); struct (@objc); struct (@objC); We can only create another class and write a method in that class that returns the value in the struct

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development communication group: 130 595 548, no matter you are small white or big bull, welcome to enter. Let’s make progress together and develop together! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)

Pit:

  1. Every time you add @objcmembers to a class, you’ll need to recompile it to get a hint.

  2. There is a special case where I call a VIEW written by SWIFT from one OC controller, and then other swift controllers need to call OC controller, and if I just write OC controller to the bridge file, I will get an error. ———— solution: Swift write View with forward declaration (@class BannerView);

  3. For example, if an Int in swift is defined as an optional type, OC cannot call a swift Int. The solution is to define a common Int