- 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.
推薦閱讀
- 計算機組成原理與接口技術(shù):基于MIPS架構(gòu)實驗教程(第2版)
- 企業(yè)數(shù)字化創(chuàng)新引擎:企業(yè)級PaaS平臺HZERO
- 數(shù)據(jù)庫技術(shù)與應(yīng)用教程(Access)
- 數(shù)據(jù)挖掘原理與實踐
- Spark快速大數(shù)據(jù)分析(第2版)
- Voice Application Development for Android
- 大話Oracle Grid:云時代的RAC
- 企業(yè)級數(shù)據(jù)與AI項目成功之道
- 大數(shù)據(jù)精準(zhǔn)挖掘
- Visual FoxPro數(shù)據(jù)庫技術(shù)基礎(chǔ)
- R Machine Learning Essentials
- 云計算寶典:技術(shù)與實踐
- 實現(xiàn)領(lǐng)域驅(qū)動設(shè)計
- Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
- 數(shù)據(jù)挖掘與數(shù)據(jù)化運營實戰(zhàn):思路、方法、技巧與應(yīng)用