官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 涿鹿县| 隆回县| 富锦市| 永靖县| 无为县| 京山县| 固原市| 嘉黎县| 红河县| 贺州市| 永善县| 贵溪市| 都安| 西平县| 行唐县| 荣昌县| 蓬溪县| 逊克县| 廊坊市| 招远市| 白银市| 江安县| 绥芬河市| 合川市| 望谟县| 临武县| 石门县| 威远县| 松溪县| 陇南市| 定边县| 罗田县| 顺平县| 贵定县| 海南省| 莱州市| 楚雄市| 鄂尔多斯市| 蒙阴县| 介休市| 修水县|