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

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.

主站蜘蛛池模板: 阜南县| 临澧县| 南和县| 万荣县| 灯塔市| 恭城| 汉阴县| 同心县| 隆昌县| 武胜县| 河南省| 偏关县| 宾阳县| 慈溪市| 北京市| 静宁县| 邵阳县| 仙桃市| 宝山区| 永寿县| 梁山县| 高碑店市| 苗栗县| 克拉玛依市| 时尚| 德昌县| 新晃| 顺义区| 浑源县| 盱眙县| 绵阳市| 昭苏县| 运城市| 尼木县| 罗源县| 萨嘎县| 黄浦区| 新泰市| 申扎县| 南投县| 阳曲县|