iOS The nullability qualifiers (为空性修饰符)

转自 http://www.jianshu.com/p/0aca839891fe

The nullability qualifiers 空特性 修饰符 是Xcode 6.3 引入的一种修饰符,主要用来修饰一个参数是否 可以为空, 用来 和 swift 过渡兼容。

在swift中,使用 !?来表示一个 optional 值是 non 还是 some, 表示 optional 值不为空, 表示 可能为空,可能不为空。 swift 中 ! ? 的用法 可以看这篇文章: Swift中的问号?和感叹号!

我们可以在 Xcode 的 发布说明里面看到相关的说明。


01_api输入框.png

02_侧边栏.png

nullability 为空性修饰符

The nullability qualifiers

在iOS9 中 Xcode 6.3 之后,Object-C 引入了 一种 为空性 修饰符,因此Objective-C 现在可以表示参数的 nullability 为空性。 修饰 属性 或者 变量。

为空性修饰符 中的 四种修饰符

  • nonnull 不为空
  • nullable 可以为空
  • null_unspecified 不确定是否为空
  • null_resettable setter 可以为空, getter 不为空

OC 中的为空性修饰符

在 OC 里面 我们使用 nonnullnullable 来表示 参数和属性 可以为空 或者 不可以为空。

例如,以下是 OC UITableView APIs 里面的一些 为空性 语句:

-(void)registerNib:(nonnull UINib *)nib forCellReuseIdentifier:
                   (nonnull NSString *)identifier;
-(nullable UITableViewCell *)cellForRowAtIndexPath:
                   (nonnull NSIndexPath)indexPath;
@property (nonatomic, readwrite, retain, nullable) UIView *backgroundView;

在 上面的方法参数里面。方法1 identifier 参数不能为空。 方法2 indexPath 参数不能为空。 属性 backgroundView 可以为空。

swift 中的为空性修饰符 ! ?

OC 中的为空性修饰符 与 swift中 optional类型数据的为空性相似。在 swift 中 使用 ? 表示 可以为空, 使用 ! 表示 不为空,

swift 中 对一个 optional 类型 使用 ! 不为空修饰 称为 implicitly-unwrapped optionals (隐式拆包optioanls) 。

swift 中的 non-optional 类似于 OC 的 nonnull-qualified types ,例如( UINib!)。

swift 中的 optional 类似于 OC 的 nullable-qualified types, 例如 ((e.g., UITableViewCell?))。

swift APIs 中 的 为空性 修饰 语句:

func registerNib(nib: UINib, forCellReuseIdentifier identifier: String)
func cellForRowAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell?
var backgroundView: UIView?

在以上代码中, UIView 或许为空, UITableViewCell 或许为空

指针类型 与 为空性修饰符

为空性修饰符 可以修饰 所有的指针类型,包括 C指针类型,block 类型,以及C++ 指针类型, 为空性修饰符 修饰 指针类型时 使用双下划线,__nonnull __nullable

例如:在 C api 中 在block中的用法

void enumerateStrings(__nonnull CFStringRef (^ __nullable callback)(void));

callback 返回值 可以为空, CFStringRef函数 返回值不为空,在swift中这样使用

func enumerateStrings(callback: (() -> CFString)?)

三种不同类型的为空性修饰符

我们来总结以下, 为空性修饰符,一共有三种。

  1. 指针 使用 双下滑线 __nonnull nullable
  2. OC 中使用 nonnull nullable
  3. swift 中使用 ! ?

03_为空性修饰符.png
Type qualifier spellingObjective-C property/method spellingSwift viewMeaning
__nonnullnonnullNon_optioanl,如 UINib该值永远不会为nil(有一种例外是可能参数传递时传入的消息为空)
__nullablenullableOptional,如 UITAbleViewCell?该值可能为nil
__null_unspecifiednull_unspecified隐式拆包可选类型 如NSDate!不确定该值是否为空(很少用)

不为空修饰宏

在OC中还提供了一个不为空修饰宏 NS_ASSUME_NONNULL_BEGNNS_ASSUME_NONNULL_END。 在接口中 nullable 的是少数,因此为了防止写一大堆 nonnull,Foundation 还提供了一对宏,包在宏里面的对象默认加 nonnull 不为空修饰符,只需要把 nullable 为空的指出来就行。

如下所示:

NS_ASSUME_NONNULL_BEGIN
// …
-(void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier;
-(nullable UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath)indexPath;
@property (nonatomic, readwrite, retain, nullable) UIView *backgroundView;
// …
NS_ASSUME_NONNULL_END

在以上代码中 参数 nib、identifier、indexPath 虽然没有 添加 为空性修饰,但是在 不空空修饰宏中,因此 为 nonull。 backgroundView 为 nullable。

注意点:

  • typedef 定义的类型不会继承 nullability 特性, 而是根据上下文选择nullablenon-nullable ,所以,就算是在 宏内,typedef定义的类型也不会被当作nonnull。

null_resettable

null_resettable 是用来修饰 属性的, 被 null_resettable 修饰的属性, setter 返回值 修饰符为 nullablegetter 返回值 修饰符为 nonable. 也就是说 , setter 可以为空, getter 不能为空, (比如某些属性提供了默认值),此时使用 null_resettable 修饰属性 。
例如,UIViewtintColor 属性,当我们没有指定 填充色的时候,使用系统默认的填充色。

@property (nonatomic, retain, null_resettable) UIColor *tintColor;

此 属性变量在 swift API 中 为 隐式解包

var tintColor: UIColor!


文/Shumin_Wu(简书作者)
原文链接:http://www.jianshu.com/p/0aca839891fe
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Preface Building and maintaining a network involves more than just making sure that packets can flow between devices on the network. As a network administrator, you also want to ensure that only the right people can access resources on your network, and that your network will continue to run even if parts of that network fail or are configured incorrectly. Your organization may have directives that you need to implement, like using cheaper network paths whenever possible. In short, while maintaining connectivity is important, you also need to implement security, robustness, and business policies with your network. This book is about network policies and how to implement those policies using Cisco IOS access lists. I present a way to think about access lists and network policy, describe how access lists are built, and give examples of how to apply those access lists in different situations. Along the way, there are a number of sidebars and notes about concepts and information important to using access lists, and at the end of the book, there are appendixes with useful reference material. A brief note about what I cover: the access lists in this book deal only with the Internet Protocol (IP), though you could probably use many of the same techniques with other network protocols as well. While all the examples involve Cisco IOS access lists, many of the concepts are generic and can be applied to other router vendors' equipment. I've tried to make the examples in this book applicable to as many IOS versions as possible; most examples should work with Versions 10.* and above. If a feature is only available later or is known to fail with certain platforms and versions, I try to point that out. Please note, also, that the terms "access list" and "access control list" are used interchangeably throughout the book. It is unfortunate that the general policy mechanism for Cisco routers is known as an access list. The term access connotes that access lists apply only to the area of security, while in fact access lists are used for a whole range of policies, not just for security concerns. I envision this book as a guide and reference for implementing network policies with access lists on Cisco routers. Cisco IOS Access lists Page 6 Organization Chapter 1, motivates our discussion of access lists by giving examples of why you need to implement network policies. It then describes a framework for thinking about access lists and provides an idea of how we use access lists and the tools for implementing policy. Chapter 2, describes access list fundamentals: the format of the basic types, masking, and ways to maintain access lists. It also discusses some tricks and traps of access lists (like the difference between network masks and access list masks), some common mistakes, and ways to reduce the number of access list entries and access list changes you may need to make. Chapter 3, shows how to use access lists to implement security policies. It has examples of access lists that control access to router resources and to hosts, and discusses the tradeoffs of different kinds of access lists. The chapter includes explanations of how certain protocols work and ends with a discussion of access list alternatives. Chapter 4, describes using access lists to control routing. Network administrators typically use access lists for routing to make sure that their networks are robust and to implement business policy decisions; I include a number of examples demonstrating these tasks. Chapter 5, is about (what else?) debugging access lists. It first goes over how to check that your access lists are correct, and then shows what to do if you discover that they are wrong. Chapter 6, describes more advanced forms of access lists, including community lists, AS path access lists, and route maps. The chapter goes over policy routing and ends with a discussion of using access lists and routes with BGP, the Border Gateway Protocol. Chapter 7, concludes the book with some case studies of how different types and applications of access lists are used together in a variety of scenarios. There are three cases: an example of routers that connect sites within an organization, a firewall example, and a BGP routing example. Appendix A, has a number of tables listing keywords and qualifiers for extended access lists. Appendix B, contains a decimal/binary conversion chart and a table of prefix lengths and their corresponding network masks, access list masks, and valid networks. Appendix C, contains a table of commonly used application ports. Cisco IOS Access lists Page 7 Audience This book is designed for network administrators and others who use Cisco routers to implement policies, whether the policies are for security or to ensure that networks are robust. Basic knowledge of Cisco routers and TCP/IP is assumed. Those who are relatively new to using Cisco routers should start with Chapter 1 and work their way through Chapter 5. Network administrators who need to implement policy-based routing using route maps, whether with interior routing protocols or with BGP, should read Chapter 6. Chapter 7 contains case studies that readers may find useful. Administrators who are experienced in using Cisco routers can use this book as a reference for policy implementation, debugging, and access lists in general. Chapter 2 describes masking techniques that may reduce access list sizes and reduce the number of necessary changes. Chapter 3, Chapter 4, Chapter 6, and Chapter 7 have many examples of implementing basic security, robustness, and business policies. Readers interested in debugging access list problems should find Chapter 5 useful. The three appendixes contain helpful reference tables of access list keywords, decimal to binary conversions, and masks and ports that common applications use. Network administrators may find the table showing network masks, access list masks, and valid networks for each possible prefix length particular useful.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值