桥接头有什么用?我们应该避免使用桥接头吗?
What is the use of bridging header? Should we avoid using bridging header?
桥接头有什么用?
是否只是为了在同一项目中使用 Objective-C 和 Swift 代码?
我们应该避免使用桥接头吗?
比如说,如果有两个非常相似的第三方库;其中一个在 Objective-C 中,另一个在 Swift 中。我们应该使用 Swift 库还是使用 Objective-C 库。使用桥接头有什么缺点吗?
不,在您的 Swift 项目中使用 Obj-c 代码没有任何缺点。桥接 header 只会将您的 Obj-c 文件公开给 Swift。这两种语言可以毫无问题地共存于同一个项目中,因为您可以将您的 Swift 代码公开给 Obj-c 也同样容易 - xCode 将生成一个 header您所有的 public Swift 声明。尽管一切皆有可能,但如果你开始一个新项目,你应该坚持使用一种语言,这样项目更容易理解。例如,如果您决定使用 Swift,您应该只对 Swift.
中不可用的库使用 Obj-c
问题:桥接有什么用header?
正确地说,桥接 header 允许用户在同一项目的 swift 代码中使用 Objective-C classes/files。
A Swift bridging header allows you to communicate with your old Objective-C classes from your Swift classes. You will need one if you plan to keep portions of your codebase in Objective-C. It should be noted that even if you decide to convert all of your code to Swift, some classes or libraries you may use such as SVProgressHUD
haven’t been rewritten in Swift and you will need to use a bridging header to use them.
问题:我们是否应该避免使用桥接 header?
考虑到您的问题,有 2 种可能的情况。
案例 1 : 假设您的项目是在 Objective-C 开发的,现在您正在使用 swift 开发新功能,在这种情况下您必须具有 BridgingHeader
,因为您需要在 swift 代码中访问 Objective-C 类。
案例 2 : 如果您的项目是在 swift 中开发的,则无需桥接 header,如果它仅在 Objective-C 并且您不打算将其移动到 swift 那么您也不需要它。
在 Apple 文档中阅读有关 Using swift with cocoa and Objective-C 的更多信息。
Apple 写了一本很好的书,深入介绍了这一点。可以在这里找到:
我会引用它来回答你的问题:
"桥接头有什么用?
是否只是为了在同一项目中使用 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 在同一个项目中一起工作。
"Should we avoid using bridging header? Say, if there are two third party library which are very similar; one of them is in Objective-C and other is in Swift. Should we use the Swift library or use Objective-C library. Are there any downside of using bridging headers?"
总有取舍。对此的第一个答案是否定的,您不应该避免使用桥接头;但是,就第三方库而言,您必须考虑许多因素。哪个功能更多?是否正在维护 and/or 经常添加?
使用 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).
What is the use of bridging header?
- 你已经得到了答案。你是对的。
Should we avoid using bridging header?
没有。当第三方库在 Obj-C 中开发并且可能在 Swift 中不可用时,这很好。您应该使用桥接头为您的应用程序提供最好的库。
就看你选哪个了。在联网的情况下?如果你的项目是基于 Obj-C 的,你可以使用 AFNetworking
或者在 Swift 的情况下你可以使用 AlamoFire
,你仍然可以在 [=29= 中使用 AFNetworking
] 但它不可建议。
桥接 header 是获得 Objective - C 代码到您的 Swift 项目中的好方法。如果您有两个库,一个在 Swift 中,一个在 Objective - C 中,请选择将在您的应用程序中提供更多功能的那个。如果它们提供相同的功能,我会选择 Swift 库 -> 我的推理:如果 Objective-C 库没有被广泛使用并且没有太多关于如何转换的教程Objective - C 代码转换为 Swift,您自己弄清楚它可能非常耗时。如果您使用 Swift 库,则代码已经以正确的语言格式化,从而节省您的时间和潜在的金钱(取决于您是否有兴趣)。至于使用桥接 header 的任何缺点,它们确实没有!有这么多用 Objective-C 编写的库,您几乎需要在您的应用程序中桥接 header。以 Reachability(Here is a video on implementation in Swift). This is a library that Apple created to handle network interruptions in your app. This is a great tool for developers and requires a bridging header. Here 是关于如何使用桥接 header 的精彩 YouTube 视频为例,但是如果您将 header 文件添加到 Swift 文件中,Xcode 通常会要求为您创建一个。希望这对您有所帮助!
The bridging header allows the use of Swift and Objective-C in the same project. There are no downsides to having a bridging header in your project as the two languages can work well together within the same app.
在添加桥接头后从项目中删除它可能会导致错误,因为它在项目的其他地方被引用创建时的项目。
If you only intend to use one of the two languages, a bridging header is unnecessary. On the other hand, if you are using both Swift and Objective-C, a bridging header is required and will not cause any issues.
这里是 link 以查找有关该主题的更多信息:
我希望这回答了你的问题。祝你的项目好运!
桥接头有什么用?
是否只是为了在同一项目中使用 Objective-C 和 Swift 代码?
我们应该避免使用桥接头吗?
比如说,如果有两个非常相似的第三方库;其中一个在 Objective-C 中,另一个在 Swift 中。我们应该使用 Swift 库还是使用 Objective-C 库。使用桥接头有什么缺点吗?
不,在您的 Swift 项目中使用 Obj-c 代码没有任何缺点。桥接 header 只会将您的 Obj-c 文件公开给 Swift。这两种语言可以毫无问题地共存于同一个项目中,因为您可以将您的 Swift 代码公开给 Obj-c 也同样容易 - xCode 将生成一个 header您所有的 public Swift 声明。尽管一切皆有可能,但如果你开始一个新项目,你应该坚持使用一种语言,这样项目更容易理解。例如,如果您决定使用 Swift,您应该只对 Swift.
中不可用的库使用 Obj-c问题:桥接有什么用header?
正确地说,桥接 header 允许用户在同一项目的 swift 代码中使用 Objective-C classes/files。
A Swift bridging header allows you to communicate with your old Objective-C classes from your Swift classes. You will need one if you plan to keep portions of your codebase in Objective-C. It should be noted that even if you decide to convert all of your code to Swift, some classes or libraries you may use such as
SVProgressHUD
haven’t been rewritten in Swift and you will need to use a bridging header to use them.
问题:我们是否应该避免使用桥接 header?
考虑到您的问题,有 2 种可能的情况。
案例 1 : 假设您的项目是在 Objective-C 开发的,现在您正在使用 swift 开发新功能,在这种情况下您必须具有 BridgingHeader
,因为您需要在 swift 代码中访问 Objective-C 类。
案例 2 : 如果您的项目是在 swift 中开发的,则无需桥接 header,如果它仅在 Objective-C 并且您不打算将其移动到 swift 那么您也不需要它。
在 Apple 文档中阅读有关 Using swift with cocoa and Objective-C 的更多信息。
Apple 写了一本很好的书,深入介绍了这一点。可以在这里找到:
我会引用它来回答你的问题:
"桥接头有什么用? 是否只是为了在同一项目中使用 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 在同一个项目中一起工作。
"Should we avoid using bridging header? Say, if there are two third party library which are very similar; one of them is in Objective-C and other is in Swift. Should we use the Swift library or use Objective-C library. Are there any downside of using bridging headers?"
总有取舍。对此的第一个答案是否定的,您不应该避免使用桥接头;但是,就第三方库而言,您必须考虑许多因素。哪个功能更多?是否正在维护 and/or 经常添加?
使用 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).
What is the use of bridging header?
- 你已经得到了答案。你是对的。
Should we avoid using bridging header?
没有。当第三方库在 Obj-C 中开发并且可能在 Swift 中不可用时,这很好。您应该使用桥接头为您的应用程序提供最好的库。
就看你选哪个了。在联网的情况下?如果你的项目是基于 Obj-C 的,你可以使用
AFNetworking
或者在 Swift 的情况下你可以使用AlamoFire
,你仍然可以在 [=29= 中使用AFNetworking
] 但它不可建议。
桥接 header 是获得 Objective - C 代码到您的 Swift 项目中的好方法。如果您有两个库,一个在 Swift 中,一个在 Objective - C 中,请选择将在您的应用程序中提供更多功能的那个。如果它们提供相同的功能,我会选择 Swift 库 -> 我的推理:如果 Objective-C 库没有被广泛使用并且没有太多关于如何转换的教程Objective - C 代码转换为 Swift,您自己弄清楚它可能非常耗时。如果您使用 Swift 库,则代码已经以正确的语言格式化,从而节省您的时间和潜在的金钱(取决于您是否有兴趣)。至于使用桥接 header 的任何缺点,它们确实没有!有这么多用 Objective-C 编写的库,您几乎需要在您的应用程序中桥接 header。以 Reachability(Here is a video on implementation in Swift). This is a library that Apple created to handle network interruptions in your app. This is a great tool for developers and requires a bridging header. Here 是关于如何使用桥接 header 的精彩 YouTube 视频为例,但是如果您将 header 文件添加到 Swift 文件中,Xcode 通常会要求为您创建一个。希望这对您有所帮助!
The bridging header allows the use of Swift and Objective-C in the same project. There are no downsides to having a bridging header in your project as the two languages can work well together within the same app.
在添加桥接头后从项目中删除它可能会导致错误,因为它在项目的其他地方被引用创建时的项目。
If you only intend to use one of the two languages, a bridging header is unnecessary. On the other hand, if you are using both Swift and Objective-C, a bridging header is required and will not cause any issues.
这里是 link 以查找有关该主题的更多信息:
我希望这回答了你的问题。祝你的项目好运!