iOS 9新特性关键字:nonnull、nullable、null_resettable
注意这几个关键字都是用来修饰对象类型的!
一、nonnull
1、作用:表示不能为空
2、举例说明:
- 属性
nonnull 声明的属性不能为空(getter方法和setter方法都有)
@property (nonnull, nonatomic, copy) NSString *name;//写法一
@property (nonatomic, copy) NSString *__nonnull name;//写法二,小写时为两个下划线
@property (nonatomic, strong) NSString *_Nonnull name;//写法三,大写时为一个下划线
这里需要注意一个问题:
person.name = nil;//系统会有警告不能给这个属性赋nil
NSString *string = nil;
[person setName:string];//这里系统不会识别到
如下图所示

-
每个属性都要写关键字很麻烦,用下面的方法
NS_ASSUME_NONNULL_BEGIN @interface ViewController : UIViewController @property (nonatomic, weak) UILabel *label; @property (nonatomic, weak) UIButton * __nullable button; @property (null_resettable, nonatomic, strong) NSArray *friends; @end NS_ASSUME_NONNULL_END 在NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END之间,定义的所有对象属性和方法 默认都是nonnull NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_END 要成对出现,不然报错 一般用于头文件.h 将声明包含起来针对所有属性添加 nonnull 修饰 也就是说, 除了nullable和null_resetable需要加修饰, 其他都不需要加修饰 看下面对比图:


- 系统的代码提示
系统提示该方法的参数不能为空
[array addObject:(nonnull NSString *)];
iOS 9以后系统都添加了这些关键字,方便开发者使用,我们也应该给自己的工程添加这些关键字,这样可以很明确的告诉其他人接口中的参数是否可以为空
二、nullable
1、作用:表示可以为空
2、举例说明:
- 属性
nullable 声明的属性可以为空
@property (nullable, nonatomic, copy) NSString *gender;//写法一
@property (nonatomic, copy) NSString * __nullable gender;//写法二,小写时为两个下划线
@property (nonatomic, strong) NSString * _Nullable gender;//写法三,大写时为一个下划线 - 系统代码提示
[person setGender:(NSString * _Nullable)];
下方是系统头文件中的方法,参数的关键字为nullable
- (NSString *)uppercaseStringWithLocale:(nullable NSLocale *)locale
三、null_resettable
1、作用:setter可为空, gette不可为空
setter方法是nullable(可以赋空值),getter方法是nonnull(取值不能为空)
2、举例说明:
- 属性
view是UIViewController头文件中的属性
@property(null_resettable, nonatomic,strong) UIView *view;
tintColor是UIView头文件中的属性
@property(null_resettable, nonatomic, strong) UIColor *tintColor - 当看到由null_resettable修饰的属性时,就应该猜想这个属性的初始化采用了懒加载方式
@property (nonatomic, strong, null_resettable) UITableView *tableView;
四、泛型
1、带泛型的容器(规定容器中所存储的数据类型)
(1)带泛型的数组
声明一个可变数组, 内部存放的都是NSString
1.数组中添加其他类型会有警告
2.数组取出来的类型不再是id类型, 会对应变成声明时的类型
3.泛型会改变类的一些方法, 使与声明的类型相同
看下图的情况:

在初始化的时候可以在数组中存储不同类型的数据
完成初始化后就只能添加声明类型的数据
取出来的类型再也不会是id类型啦,如下所示,firsObject属性有固定的类型(自己声明的类型)

声明了容器中存储的数据类型后系统的方法会有相应的提示,从
下图可以看到可变数组addObject:方法的参数有了规定的类型

(2)带泛型的字典

可以看到使用泛型规定了字典key和value的数据类型后,向字典中添加其他类型(如图中的数组)
时会有类型不匹配的警告。而从字典中取出的value也有了固定的类型(图中规定的是NSString),
因此可以通过取出来的value使用getter方法得到字符串的length属性
(3)自定义泛型
首先我们来看下什么是自定义泛型,下图是系统NSMutableArray头文件中的声明,可以看到
在类名后面添加一对< >符号,中间是自定义泛型的名字(可以自己起名),后边以 :父类名结束
声明好自定义泛型之后,头文件中方法的参数类型就可以使用自定义泛型来修饰了,如下图所示

这里自定义泛型只能在声明部分写,在.m实现文件中则需使用id类型来表示自己定义的泛型,请看下图
在自定义的Truck类.m文件中使用id来表示Truck类.h文件中的自定义泛型

五、协变性与逆变性
还是先来看看实际的例子来了解下什么是协变性和逆变性吧,下图是系统NSArray的头文件部分,可以看到它使用
了自定义泛型并命名为OBjectType,在自定义泛型前加了一个__covariant的修饰符,这个修饰符就表示协变性

__covariant - 协变性,子类型可以强转到父类型(里氏替换原则)
__contravariant - 逆变性,父类型可以强转到子类型
六、__kindof
- 一般用在方法的返回值
自定义类Truck的声明部分

自定义类Truck的实现部分

分析上图:加__kindof修饰后,该方法的返回值原本是NSArray,但是方法里边却返回了一个NSArray的子类
NSMutableArray,也就是说,加__kindof修饰后,本类及其子类都是返回,调用使用时也可以使用本类或者
本类的子类去接收方法的返回值,请看下图的方法调用:

如上图所示使用自定义的Truck类调用方法,使用NSArray和NSMutableArray来接收返回值都是正确的
- 系统中的使用请看下图:
系统的UITableViewCell的中有这样的写法

网友评论
http://blog.sunnyxx.com/2015/06/12/objc-new-features-in-2015/