- 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.
- 虛擬化與云計算
- Oracle RAC 11g實戰(zhàn)指南
- Access 2007數(shù)據(jù)庫應用上機指導與練習
- 數(shù)據(jù)化網(wǎng)站運營深度剖析
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- 數(shù)據(jù)革命:大數(shù)據(jù)價值實現(xiàn)方法、技術(shù)與案例
- 數(shù)據(jù)庫技術(shù)及應用教程
- ZeroMQ
- 菜鳥學SPSS數(shù)據(jù)分析
- Hands-On Deep Learning for Games
- 碼上行動:利用Python與ChatGPT高效搞定Excel數(shù)據(jù)分析
- ECharts數(shù)據(jù)可視化:入門、實戰(zhàn)與進階
- Learn Selenium
- 算法設(shè)計與問題求解(第2版):計算思維培養(yǎng)
- 工業(yè)大數(shù)據(jù)工程:系統(tǒng)、方法與實踐