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

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.

主站蜘蛛池模板: 凌源市| 临朐县| 牡丹江市| 东莞市| 郁南县| 临安市| 平和县| 定州市| 八宿县| 夏邑县| 苏尼特右旗| 饶阳县| 皋兰县| 海宁市| 民丰县| 济南市| 班玛县| 旌德县| 丹寨县| 宁河县| 峨眉山市| 常州市| 类乌齐县| 津市市| 科技| 响水县| 富顺县| 崇明县| 晋中市| 赣州市| 荔浦县| 隆安县| 芦山县| 望都县| 闻喜县| 泊头市| 康平县| 邹城市| 绵阳市| 常宁市| 崇义县|