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

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.

主站蜘蛛池模板: 三都| 南汇区| 交口县| 温州市| 十堰市| 延庆县| 互助| 甘洛县| 偏关县| 连云港市| 金华市| 玉树县| 万安县| 教育| 龙游县| 调兵山市| 青河县| 长白| 嘉善县| 平果县| 平利县| 中超| 平湖市| 磐安县| 宜春市| 海丰县| 京山县| 镇坪县| 巴青县| 韩城市| 盐亭县| 榆中县| 镇宁| 抚远县| 普格县| 岳池县| 明光市| 云龙县| 天水市| 徐州市| 安康市|