- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 141字
- 2021-07-02 14:45:14
Using NS_ASSUME_NON_NULL_BEGIN and NS_ASSSUME_NON_NULL_END
We can use NS_ASSUME_NON_NULL_BEGIN and NS_ASSUME_NON_NULL_END in order to express broadly that all properties, method arguments, and return types are non-optional:
NS_ASSUME_NONNULL_BEGIN
@interface AnObject: NSObject
- (NSString *)sayHello; // return @”Hello World”
- (NSString *)append:(NSString *)aString with:(NSString *)anotherString;
@end
NS_ASSUME_NONNULL_END
The following snippet shows how to use it in Swift:
// Swift 5
let object = AnObject()
let string = object.sayHello() // String
let returnValue = object.append("First String", with: "Next String")
let otherString = string.appending(" From Swift") // safe to use
assert(otherString == "Hello World From Swift")
As you can see, nothing is an optional anymore and it properly expresses the need for non-optional parameters in Objective-C.
So far, we're able to mark large portions of code as non optional, but often, you'll need to mark just a few arguments or return types as optional.
推薦閱讀
- Google Visualization API Essentials
- 從零開(kāi)始學(xué)Hadoop大數(shù)據(jù)分析(視頻教學(xué)版)
- 數(shù)據(jù)化網(wǎng)站運(yùn)營(yíng)深度剖析
- Access 2016數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用
- 數(shù)據(jù)庫(kù)系統(tǒng)原理及應(yīng)用教程(第4版)
- 深入淺出 Hyperscan:高性能正則表達(dá)式算法原理與設(shè)計(jì)
- 金融商業(yè)算法建模:基于Python和SAS
- 數(shù)據(jù)科學(xué)工程實(shí)踐:用戶行為分析與建模、A/B實(shí)驗(yàn)、SQLFlow
- 云數(shù)據(jù)中心網(wǎng)絡(luò)與SDN:技術(shù)架構(gòu)與實(shí)現(xiàn)
- 數(shù)字IC設(shè)計(jì)入門(微課視頻版)
- 大數(shù)據(jù)技術(shù)原理與應(yīng)用:概念、存儲(chǔ)、處理、分析與應(yīng)用
- 智慧城市中的大數(shù)據(jù)分析技術(shù)
- 數(shù)據(jù)庫(kù)原理與設(shè)計(jì)實(shí)驗(yàn)教程(MySQL版)
- SQL Server 2008寶典(第2版)
- 數(shù)據(jù)挖掘算法實(shí)踐與案例詳解