gpt4 book ai didi

objective-c - 桥接头有什么用?我们应该避免使用桥接头吗?

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:56 25 4
gpt4 key购买 nike

桥接头有什么用?

是否只是为了在同一个项目中使用 Objective-C 和 Swift 代码?

我们应该避免使用桥接头吗?

比如说,如果有两个非常相似的第三方库;其中一个在 Objective-C 中,另一个在 Swift 中。我们应该使用 Swift 库还是使用 Objective-C 库。使用桥接头有什么缺点吗?

最佳答案

Apple 写了一本很好的书,深入介绍了这一点。可以在这里找到:

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

我会引用它来回答你的问题:

“桥接头有什么用?只是为了在同一个项目中使用 Objective-C 和 Swift 代码吗?”

To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app.

这个问题的答案是肯定的。它只是为了让 Swift 和 Objective-C 在同一个项目中协同工作。

“我们应该避免使用桥接 header 吗?比如说,如果有两个非常相似的第三方库;其中一个在 Objective-C 中,另一个在 Swift 中。我们应该使用 Swift 库还是使用 Objective-C库。使用桥接头有什么缺点吗?”

总是有取舍的。对此的第一个答案是否定的,您不应该避免使用桥接头;但是,就第三方库而言,您必须考虑许多因素。哪个功能更多?它是否经常维护和/或添加?

使用 Objective-C 库也会增加需要注意和解决的问题。摘自本书:

Troubleshooting Tips and Reminders

Treat your Swift and Objective-C files as the same collection of code, and watch out for naming collisions.
If you’re working with frameworks, make sure the Defines Module (DEFINES_MODULE) build setting under Packaging is set to “Yes".
If you’re working with the Objective-C bridging header, make sure the Objective-C Bridging Header (SWIFT_OBJC_BRIDGING_HEADER) build setting under Swift Compiler - Code Generation is set to a path to the bridging 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 naming the Objective-C bridging header and the generated header for your Swift code. For information on product module naming, see Naming Your Product Module.
To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.
When you bring Swift code into Objective-C, remember that Objective-C won’t be able to translate certain features that are specific to Swift. For a list, see Using Swift from Objective-C.
If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types before importing the Swift generated header into the Objective-C .m file you want to use your Swift code from.
Swift declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction, @IBOutlet, or @objc as well.
For app targets, declarations marked with the internal modifier appear in the generated header if the app target has an Objective-C bridging header.
For framework targets, only declarations with the public modifier appear in the generated header. You can still use Swift methods and properties that are marked with the internal modifier from within the Objective-C part of your framework, as long they are declared within a class that inherits from an Objective-C class. For more information on access-level modifiers, see Access Control in The Swift Programming Language (Swift 2.2).

关于objective-c - 桥接头有什么用?我们应该避免使用桥接头吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36441998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com