What is the use of a header jumper? Shouldn't we use a headline?

What is bridge header usage?

Is it easy to use Objective-C and Swift code in the same project?

Should we avoid using the bridge header?

Let's say if there are two third-party libraries that are very similar; one of them is in Objective-C, and the other is in Swift. Should we use the Swift library or use the Objective-C library. Is there a drawback to using bridge headers?

+5
source share
6 answers

Apple has written a wonderful book that details this. It can be found here:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

I will bring it to answer your questions:

"What is the use of the header? Is it easy to use Objective-C and Swift code in the same project?"

To import a set of Objective-C files in the same target application as your Swift code, you rely on the Objective-C bridge header to output these files to Swift. Xcode suggests creating this header file when adding a Swift file to an existing Objective-C application or an Objective-C file to an existing Swift application.

The answer to that is yes. This is where Swift and Objective-C work together in the same project.

โ€œShould we avoid using the bridge header? Let's say if there are two third-party libraries that are very similar, one is in Objective-C and the other is in Swift. Should we use the Swift library or use the Objective-C library. There is are there any flaws in using bridge headers? "

There are always tradeoffs. The first answer to this question is: you should not avoid using the bridge header; however, with respect to third-party libraries, you have to look at many factors. Who has more functionality? Is it supported and / or added frequently?

Using the Objective-C library will also add things you need to know about and get around. From book:

Troubleshooting Tips and Reminders

Treat your Swift and Objective-C files as the same code collection, and watch out for conflict naming.
If you are working with frameworks, make sure that the "Configure Modules" (DEFINES_MODULE) parameter in the "Packaging" section is set to "Yes".
If you are working with the Objective-C bridge header, make sure that the Objective-C Bridging Header assembly parameter (SWIFT_OBJC_BRIDGING_HEADER) in the Swift Compiler - Code Generation is set to the path to the bridge header file relative to your project (for example, "MyApp" / MyApp -Bridging- Header.h ").
Xcode uses your product module name (PRODUCT_MODULE_NAME) - not your target name (TARGET_NAME) - when assigning the Objective-C bridge header and the generated header for your Swift code. For information on naming a product module, see the "Naming Your Product" section.
To be accessible and useful in Objective-C, the Swift class must be a descendant of the Objective-C class or must be marked as @objc.
When you enter Swift code in Objective-C, remember that Objective-C will not be able to translate some functions specific to Swift. For a list, see Using Swift from Objective-C.
If you use your own Objective-C types in your Swift code, be sure to import the Objective-C headers for these types before importing the generated Swift header into the Objective-Cm file from which you want to use Swift code. <w> Swift scrolls marked with a private modifier do not appear in the generated header. Private ads are not exposed to Objective-C unless explicitly tagged with @IBAction, @IBOutlet or @objc.
For target applications, ads marked with an internal modifier are displayed in the generated header if the target of the application has an Objective-C bridge header.
For the purpose of the frames, only ads with an open modifier appear in the generated heading. You can still use the Swift methods and properties that are marked with the internal modifier from the Objective-C part of your framework, as they are declared in a class that inherits from the Objective-C class. For more information about access level modifiers, see Access Control in Swift Programming (Swift 2.2).

+7
source

No, there are no flaws in using Obj-c code in your Swift project. The Bridging header provides only Obj-c files for Swift. These two languages โ€‹โ€‹can coexist in the same project without any problems, since you can easily present your Swift code in Obj-c too - xCode will create a header for all your Swift declarations. Although everything is possible, if you are starting a new project, you should stick to one language, so the project is easier to understand. For example, if you decide on Swift, you should use Obj-c for libraries that are not available in Swift.

+2
source

Que: What is the use of the bridge header?

Itโ€™s right to say that the Bridging header allows you to use Objective-C classes / files in your fast code in one project.

The Swift summary header allows you to talk to the old Objective-C classes from your Swift classes. You will need one if you plan on storing parts of your code base in Objective-C. It should be noted that even if you decide to convert all your code to Swift, some classes or libraries that you can use, such as SVProgressHUD havent, have been overwritten in Swift, and you will need to use the bridge header to use them.

Que: should I use a bridge header?

Given your question, 2 cases are possible.

case 1: Let's say your project is developed in Objective-C, and now you are developing new functions using swift in it, in this case you should have a BridgingHeader , since you need access to your Objective-C classes in fast code.

case 2: If your project is developed in swift, then there is no need to have a Bridging header, and also if it is only in Objective-C, and you do not plan to move it to swift, then you do not need it either.

Read more about Using swift with cocoa and Objective-C in the Apple documentation.

The following image from an Apple document indicates the use of the Bridging header. Header modification

+2
source

The bridge header allows you to use Swift and Objective-C in the same project. There are no flaws in your project related to combining the header, as two languages โ€‹โ€‹can work well together in one application.

Removing the bridge title from the project after adding it can lead to errors , since in other places in the project they are referenced.

If you intend to use only one of the two languages, the bridge header is not needed. On the other hand, if you use Swift and Objective-C, a bridge header is required and will not cause any problems.

Here is the link to find additional information on the topic:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

I hope that answered your question. Good luck with your project!

+2
source

What is bridge header usage?

  • You already have this answer. You're right.

Should the bridge header be avoided?

  • No. This is good when a third-party library appeared in Obj-C, which is not yet available in Swift. You have to use a bridge for the header to have the best library for your application.

  • It depends on what you choose. In case of network connection? If your project is based on Obj-C, you can use AFNetworking , or in the case of Swift, you can use AlamoFire , you can still use AFNetworking in Swift, but it is not recommended.

+1
source

Bridge headers are a great way to get Objective-C code into your Swift project. If you have two libraries, one in Swift and one in Objective-C, choose the one that offers more functionality in your application. If they offer the same functionality, I would just go with the Swift library โ†’ My reasoning: if the Objective-C library is not widely used and there are not many tutorials on how to convert Objective-C code to Swift, it can be very time consuming figure it out on your own. If you use the Swift library, the code is already formatted in the correct language, which saves you time and potential money (depending on whether this is a hobby for you or not). As for any drawbacks to using the bridge header, they really aren't! With so many libraries written in Objective-C, you almost need a title bar in your application. Take Reachability, for example ( Here is the Swift implementation video). This is a library created by Apple to handle network interrupts in your application. This is a great tool for developers and requires a bridge header. Here 's a great YouTube video on how to use the bridge header, but if you add the header file to your Swift file, Xcode usually asks for the one key for you. Hope this helps!

+1
source

Source: https://habr.com/ru/post/1246517/


All Articles