- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 245字
- 2021-07-02 14:45:14
Exposing Swift to Objective-C
In order to expose Swift to Objective-C, Xcode will generate a header for you, which contains all Swift classes and modules that can be exposed to Objective-C. In Xcode, you can set the value of that header by controlling SWIFT_OBJC_INTERFACE_HEADER_NAME.
There are many reasons why a Swift class can't be exposed to Objective-C, including the following:
- Using types that don't directly bridge to Objective-C
- Using generics
- Using pure Swift structs and classes
SWIFT_OBJC_INTERFACE_HEADER_NAME is usually set to $(SWIFT_MODULE_NAME)-Swift.h at the project level, which is a good default and unlikely to conflict with any other header declaration. Most of the time, $(SWIFT_MODULE_NAME) will be the name of your app.
Whenever you need to import Swift code in your Objective-C implementation files use the following:
#import "MyProject-Swift.h"
This will properly import all of your Objective-C compatible declarations from Swift.
Now that we have properly bridged the two worlds of Swift and Objective-C, let's explore the interoperability layer with one of the most important features of Swift, nullability. In Objective-C, sending a message to nil or calling a method on a nil object has no effect, but it's unlikely that the developer on purpose left that in the code. Unlike Java, the program will not crash, nor raise an exception. In Swift, calling a method on a nil value results in a crash. As Objective-C can produce nil values, we need to reconcile these two worlds with nullability annotations.
- 數據要素安全流通
- 數據浪潮
- Access 2016數據庫教程(微課版·第2版)
- Access 2007數據庫應用上機指導與練習
- Access 2016數據庫技術及應用
- Oracle高性能自動化運維
- WS-BPEL 2.0 Beginner's Guide
- 智能數據時代:企業大數據戰略與實戰
- 數據中心數字孿生應用實踐
- 編寫有效用例
- Python數據分析與數據化運營
- Solaris操作系統原理實驗教程
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 聯動Oracle:設計思想、架構實現與AWR報告
- Expert Python Programming(Third Edition)