- 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.
- 數(shù)據(jù)庫基礎教程(SQL Server平臺)
- 數(shù)據(jù)庫原理及應用教程(第4版)(微課版)
- ETL數(shù)據(jù)整合與處理(Kettle)
- SQL Server 2012數(shù)據(jù)庫技術與應用(微課版)
- 新型數(shù)據(jù)庫系統(tǒng):原理、架構(gòu)與實踐
- 數(shù)據(jù)驅(qū)動:從方法到實踐
- 深入淺出 Hyperscan:高性能正則表達式算法原理與設計
- 科研統(tǒng)計思維與方法:SPSS實戰(zhàn)
- 改變未來的九大算法
- 計算機視覺
- The Natural Language Processing Workshop
- Access 2016數(shù)據(jù)庫應用基礎
- 標簽類目體系:面向業(yè)務的數(shù)據(jù)資產(chǎn)設計方法論
- 數(shù)據(jù)迷霧:洞察數(shù)據(jù)的價值與內(nèi)涵
- 敏捷數(shù)據(jù)分析工具箱:深入解析ADW+OAC