您当前的位置: 首页 >  ios

培根芝士

暂无认证

  • 1浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

iOS9新特性关键词

培根芝士 发布时间:2018-12-29 15:17:34 ,浏览量:1

nonnull

1、作用:表示不能为空

2、用法:

(1)属性

nonnull 声明的属性不能为空(getter方法和setter方法都有)

@property (nonnull, nonatomic, copy) NSString *name; //写法一
@property (nonatomic, copy) NSString *__nonnull name; //写法二,小写时为两个下划线
@property (nonatomic, copy) 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需要加修饰, 其他都不需要加修饰

(2)参数

系统提示该方法的参数不能为空

[array addObject:(nonnull NSString *)];
[array addObject:(NSString * __nonnull)];
[array addObject:(NSString * _Nonnull)];
nullable

1、作用:表示可以为空

2、举例说明:

(1)属性

nullable 声明的属性可以为空

@property (nullable, nonatomic, copy) NSString *gender; //写法一
@property (nonatomic, copy) NSString * __nullable gender; //写法二,小写时为两个下划线
@property (nonatomic, strong) NSString * _Nullable gender; //写法三,大写时为一个下划线

(2)参数

[person setGender:(NSString * _Nullable)];
[person setGender:(NSString * __nullable)];
[person setGender:(NSString * _Nullable)];
null_resettable

1、作用:setter可为空, getter不可为空

setter方法是nullable(可以赋空值),getter方法是nonnull(取值不能为空)

2、用法:

(1)属性

用null_resettable来修饰属性,表示这个属性的初始化采用了懒加载方式

@property(null_resettable, nonatomic, strong) UIView *view;

 

关注
打赏
1660824269
查看更多评论
立即登录/注册

微信扫码登录

0.0398s