Xcode10 NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END

简介: 前言升级成 Xcode 10 之后每次 New File 看到 .h 基本都能看到 NS_ASSUME_NONNULL_BEGIN 和 NS_ASSUME_NONNULL_END 成对出现在 @interface 与 @end 上下, 包裹住它, 这两对关键字并非新特性, 只是 Xcode 10 之后系统默认实现了, 应该是考虑到与 Swift 混编, 为了更好兼容其 optional 与 non-optional。

前言

升级成 Xcode 10 之后每次 New File 看到 .h 基本都能看到 NS_ASSUME_NONNULL_BEGINNS_ASSUME_NONNULL_END 成对出现在 @interface 与 @end 上下, 包裹住它, 这两对关键字并非新特性, 只是 Xcode 10 之后系统默认实现了, 应该是考虑到与 Swift 混编, 为了更好兼容其 optional 与 non-optional。

原理

我们直接来看定义: NSObjectRuntime.h

#ifndef NS_ASSUME_NONNULL_BEGIN
#define NS_ASSUME_NONNULL_BEGIN _Pragma("clang assume_nonnull begin")
#endif
#ifndef NS_ASSUME_NONNULL_END
#define NS_ASSUME_NONNULL_END   _Pragma("clang assume_nonnull end")

其表示由 NS_ASSUME_NONNULL_BEGIN 开始以下定义的 对象都是 __nonnull 不可为空的, 直到 NS_ASSUME_NONNULL_END 作用域。

例如:


NS_ASSUME_NONNULL_BEGIN

@interface YZHAddBookDetailsVC : YZHBaseViewController

@property (nonatomic, copy) NSString* name;
@property (nonatomic, strong) NSArray* array;

@end

NS_ASSUME_NONNULL_END

其表示 name 和 array 都是由 __nonnull 修饰, 不可为空, 当编译时如果为空会受到警告。
与其 对应的是 __nullable, 表示对象也可以为 nil。

希望此篇文章对您有所帮助,如有不对的地方,希望大家能留言指出纠正。谢谢!!!!!

学习的路上,与君共勉!!!

目录
相关文章
go time.After() time.Ticker
go time.After() time.Ticker
39 0
|
Linux
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
编译OpenJDK8:error: control reaches end of non-void function [-Werror=return-type]
191 0
TestRange.cs error CS0104: `Range' is an ambiguous reference between `System.Range' and Gtk.Range
TestRange.cs error CS0104: `Range' is an ambiguous reference between `System.Range' and Gtk.Range
184 0
|
Web App开发 Ubuntu jenkins
Running Firefox as root in a regular user's session is not supported 问题解决
Running Firefox as root in a regular user's session is not supported 问题解决
830 0
Running Firefox as root in a regular user's session is not supported 问题解决
Sap Ds Data is not available. Increase the time-out interval values in Debug | Options
Sap Ds Data is not available. Increase the time-out interval values in Debug | Options
139 0
|
iOS开发
Xcode消除警告:-1: The iOS Simulator deployment target is set to 6.0, but the range of supported deplo...
Xcode消除警告:-1: The iOS Simulator deployment target is set to 6.0, but the range of supported deplo...
169 0
Xcode消除警告:-1: The iOS Simulator deployment target is set to 6.0, but the range of supported deplo...
|
Android开发
AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at
AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at
939 0
AVD Pixel_2_API_30 is already running. lf that is not the case, delete the files at