iOS 新特性关键字

1.用来修饰属性,或者方法的参数,方法的返回值

/**

nullable:表示可以传空

*/

//@property (nonatomic, strong, nullable) NSString *name;

//@property (nonatomic, strong) NSString * __nullable name;

//@property (nonatomic, strong) NSString * _Nullable name;

/**

nonnull: non:非 null : 空

*/

//@property (nonatomic, strong, nonnull) NSString *icon;

//@property (nonatomic, strong) NSString * __nonnull icon;

//@property (nonatomic, strong) NSString * _Nonnull icon;

//方法中书写规范

/**

  • (nullable NSString *)test:(NSString *_Nullable)test;

  • (nonnull NSString *)test1:(NSString *_Nonnull)test1;

*/

/**

//在 NS_ASSUME_NONNULL_BEGIN 和 NS_ASSUME_NONNULL_END 之间定义的所有属性和方法默认都是 nonnull

NS_ASSUME_NONNULL_BEGIN

@property (nonatomic) NSString *name;//这样默认表示非空

NS_ASSUME_NONNULL_END

*/

/**

  • null_resettable :get方法返回值不能为空 set方法可以为空

  • 如果使用了这种方法必须重写 set 方法或者重写 get 方法处理传递值为空的情况

*/

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

/**

  • null_unspecified: 不确定为空

*/

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

好处:

1.迎合 swift

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

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

2.iOS9 泛型

/**

 泛型:限制类型

 泛型使用场景

 1.在集合(数组, 字典, NSSet)中使用比较常见

 2.当声明一个类,类里面的某些属性的类型不确定,这时候我们才使用泛型

 

 

 泛型书写规范

 @property (nonatomic, strong) NSMutableArray<NSString *> *data;

 在类型的后面定义泛型 NSMutableArray<NSString *> *data;



 泛型修饰:只能修饰方法的调用

 泛型好处:

 1.提高开发规范,减少程序之间交流

 2.通过集合取出来对象,直接当做泛型对象使用,可以使用点语法

 */

__kindof
/**

 * __kindof:表示当前类或者它子类

 * __kindof书写格式

 * 放在类型前面修饰这个类型

 +(__kindof person *)person;

 __kindof : 在调用的时候很清楚的知道返回的类

 */



/**

 * id 坏处: 1. 不能再编译的时候检查真是类型

 *          2.返回值,没有提示

   instancetype: 会自动识别当前对象的类,但是返回值还是没有提示

 */
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容