iOS9新特性之新添加的关键字

iOS9 新出的关键字:用来修饰属性,或者方法的参数,返回值

好处:1.迎合swift

     2.提高我们开发人员开发规范,减少程序员之间的交流

注意:iOS9新出的的关键字nonnull,nullable,null_unspecified,null_resettable只能修饰对象,不能修饰基本数据类型

 nullable : 修饰的对象可以为空

书写方式:

 1.@property (nonatomic , strong) NSString * __nullable company;

 2.@property (nonatomic , strong , nullable) NSString *company;

 3.@property (nonatomic , strong ) NSString *_Nullable company;

 nonnull  : 修饰的对象不可以为空

 书写方式:

 1.@property (nonatomic , strong) NSString * __nonnull company;

 2.@property (nonatomic , strong , nonnull) NSString *company;

 3.@property (nonatomic , strong ) NSString *_Nonnull company;

 null_resettable  : set方法参数可以为空,get方法返回值不能为空,用孩关键字修饰的对象,必须重写set或get方法处理为空情况

 书写方式:只有这一种方式

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

 -(NSString *)company

 {

 if (_company == nil) {

 _company = @"1";

 }

 return _company;

 }

 -(void)setCompany:(NSString *)company

 {

 if (company == nil) {

 company = @"1";

 }

 }

null_unspecified  : 不确定是否为空

 书写方式:

   1.@property (nonatomic , strong , null_unspecified) NSString *company;

   2.@property (nonatomic , strong ) NSString * __null_unspecified company;

   @property (nonatomic , strong ) NSString * _Null_unspecified company;

 NS_ASSUME_NONNULL_BEGIN  : 宏区间,在这个区域里的对象、方法的参数或返回值都不能为空

   NS_ASSUME_NONNULL_END

 书写方式:

 NS_ASSUME_NONNULL_BEGIN

 @property (nonatomic ) NSString *name;

 @property (nonatomic , assign ) int age;

 -(NSString *)getStr:(NSString *)str;

 NS_ASSUME_NONNULL_END

原文地址:https://www.cnblogs.com/culing/p/5714372.html