nullable,nonnull, null_resettable以及_Null_unspecified的区别和使用

时间:2023-03-08 21:39:31

1.关键字:可以用于属性 方法和返回值参数中

关键字作用:提示作用  告诉开发者属性信息

关键字的目的:迎合swift 强语言,swift必须要指定一个对象是否为空

关键字好处:提高代码规划,减少沟通成本

仅仅是提供警告并不会报编译错误

nullabel作用 :提示可能为空

语法 1

@property(nonatomic ,strong ,nullabel)NSString * name;

语法 2

@property(nonatomic ,strong )NSString * _Nullabel  name;

语法 2

@property(nonatomic ,strong )NSString * __nullabel  name;

nonnull作用 :提示不能为空

语法 1

@property(nonatomic ,strong ,nonnull )NSString * name;

语法 2

@property(nonatomic ,strong )NSString * _Nonnull  name;

语法 2

@property(nonatomic ,strong )NSString * __nonnull  name;

null_resettable 作用:必须要处理为空的情况  get不能返回nil  set方法可以传入为空

重写get方法

@property(nonatomic ,strong,null_resettable )NSString *  name;

不可以用语法2 写法  会报错

_Null_unspecified : 不确定是否为空

很少使用

—————————

!_view  改为  _view = nil  因为swift不支持前者

补充 在宏(NS_ASSUME_NONULL_BEGIN /END)之间默认是nonnull   strong

关键字不能用于基本数据类型

************************************************************************************

2.泛型

为什么要退出泛型?迎合swift

泛型好处:1.限制类型  2.提高代码规范 减少沟通成本

泛型用法:类型<限制类型>

泛型仅仅是报警告

泛型好处  从数组中取出来可以使用点语法

给数组添加元素有提示

泛型在开发中的使用场景:1.用于限制集合类型