您的位置:首页 > 移动开发 > IOS开发

第01天实战技术(01):iOS9新特性之常见关键字

2017-03-24 00:00 441 查看
#####一、怎么去研究新特性? 1.使用新的Xcode创建项目,用旧的Xcode去打开

怎么去研究新特性? 1.使用新的Xcode创建项目,用旧的Xcode去打开
规律
Xcode8 2016    iOS10
Xcode7 2015    iOS9
Xcode6 2014    iOS8
Xcode5 2013    iOS7
Xcode4 2012    iOS6

1.出了哪些新特性 iOS9 : 关键字 : 可以用于属性、方法返回值和参数中
关键字作用 : 提示作用,告诉开发者属性信息
关键字目的 : 迎合swift,swift是个强语言,swift必须要指定一个对象是否为空
关键字好处 : 提高代码规范
关键字仅仅是提供警告,并不会爆 编译出错
null_unspecified : 不确定是否为空
null_resettable
nonnull        : 不能为空
nullable       : 可能为空

#####二、iOS9关键字

1.(
nullable
)

nullable       1.怎么使用(语法) 2.什么时候使用(作用)
nullable作用 :   (从英文意思) 可能为空
nullable语法1 :   @property (nonatomic,strong,nullable) NSString *name;
nullable语法2 :  * 关键字 变量名   @property (nonatomic,strong) NSString * _Nullable age;
nullable语法3 :  XcodeBate版(测试版) @property (nonatomic,strong) NSString * __nullable height;
XcodeBate版(快速熟悉开发) :主要是练习的,建议不要去创建项目,和上传App Store(最好使用完整的版本)


2.(
nonnull
)

OC是弱语言 就算赋值不能为空 然后你强制赋为nil 也就是仅仅是警告

nonnull       1.怎么使用(语法) 2.什么时候使用(作用)
nonnull作用 :   (从英文意思) 不能为空
nonnull语法1 :   @property (nonatomic,strong,nonnull) NSString *name;
nonnull语法2 :  * 关键字 变量名   @property (nonatomic,strong) NSString * _Nonnull age;
nonnull语法3 :  XcodeBate版(测试版) @property (nonatomic,strong) NSString * __nonnull height;


3.(
null_resettable
)

// Synthesized setter 'setName:' for null_resettable property 'name' does not hangle nil
// 没有处理为空的情况

null_resettable       1.怎么使用(语法) 2.什么时候使用(作用)
null_resettable    : 必须要处理为空的情况, 重写get方法
null_resettable作用 :   (从英文意思) 可重置 但是不能明确所以需要验证 --最终的最作用是:【get方法不能返回nil,set方法可以传入为空】
控制器的view可以为空,但是他必须要有值(控制器必须要有view)
苹果是怎么处理的(懒加载)
- (UIView *)view
{
// 控制器的view 无论如何都会有值的,如果没有值就会加载出来
if (_view == nil) {
[self loadView];
[self viewDidLoad];
}
return _view;

}
null_resettable语法1 :   @property (nonatomic,strong,null_resettable) NSString *name; // 只有一种写法

// 因为人家不一定调用set方法,而有可能直接调用get方法, 所以我们在处理getter方法是最好的
- (NSString *)name
{
if (_name == nil) {
_name = @"lyh";
}
return _name;
}


code

#import "ViewController.h"

@interface ViewController ()

// nullable
@property (nonatomic,strong,null_resettable) NSString *name;
@property (nonatomic,strong) NSString * _Nonnull age;
@property (nonatomic,strong) NSString * __nonnull height;

@end

@implementation ViewController

// 在处理getter 方法是最好的
- (NSString *)name
{
if (_name == nil) {
_name = @"lyh";
}
return _name;
}

- (void)viewDidLoad {
[super viewDidLoad];

// self.name 不知道是setter 还是getter 所以 使用单独方法去访问
[self name]; // get方法 为 _Nonnull 不能为空
// self setName:<#(NSString * _Nullable)#> // set方法 _Nullable 可能为空
//    self.name = nil;
//    self.age = nil;
//    self.height = nil;

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Objective-C