iOS9新特性

iOS9新增的一些新特性

1.关键字

iOS9新出的关键字:用来修饰属性,或者方法的参数,方法的返回值
好处
1.迎合swift
2.提高开发人员的开发规范,减少程序员之间的交流
iOS9新出的关键字nonnull,nullable只能修饰对象,不能修饰基本数据类型
nullable:表示可以为空

//nullable:表示可以为空
//两种常用方式
@property(nonatomic,strong,nullable)NSString *nameStr1;
@property(nonatomic,strong)NSString *_Nullable nameStr2;

//nonnull: non:非 null:空
@property(nonatomic,strong,nonnull)NSString *icon1;
@property(nonatomic,strong)NSString *_Nonnull icon2;

//方法中关键字的书写规范
-(nonnull NSString *)test1:(nonnull NSString *)str1;
-(NSString *_Nonnull)test2:(NSString *_Nonnull)str2;

对于上面的nonnull,新的Xcode里面的UIKit框架多了NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END两个字段,这两句话的意思和nonnull一样

在NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END之间,定义的所有属性和方法默认都是nonnull,可以发现苹果的对象都是这样修饰的
 
NS_ASSUME_NONNULL_BEGIN
@property(nonatomic)NSString *nStr;
NS_ASSUME_NONNULL_END

除了上面的几个修饰符,有的对象还有null_resettable这个修饰符

/*
 null_resettable: get方法不能返回为空, set方法可以为空
 注意:如果使用null_resettable,必须重写get方法
 */
@property(nonatomic,strong,null_resettable) NSString *testStr;

重写get方法
//法一
-(NSString *)testStr{
    if (_testStr ==nil) {
        _testStr = @"重写get方法 法1";
    }
    return _testStr;
}
//法2
-(void)setTestStr:(NSString *)testStr
{
    if (testStr == nil) {
        testStr = @"重写get方法 法2";
    }
    _testStr = testStr;
}


_Null_unspecified:不确定是否为空
还有一个_Null_unspecified的修饰符,会在self.testStr的时候出现。用self.testStr的时候不确定是取值还是赋值,不确定是set还是get  没什么意义
@property(nonatomic,strong) NSString *_Null_unspecified testStr1;

2.泛型

泛型: 限制类型
泛型使用场景
1.在集合(数组,字典,NSSet)中使用泛型比较常见
2.当声明一个类,类里面的某些属性类型不确定,这时我们才使用泛型

泛型书写规范
在类型后面定义泛型,NSMutableArray *dataArr;

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

泛型好处
1.提高开发规范,减少程序员之间的交流
2.通过集合取出来的对象,直接当泛型对象使用,可以直接使用.点语法

基本的使用  下面的例子表明这个数组是字符串类型
@property(strong,nonatomic)NSMutableArray *dataArr;
拓展
定义三个对象Person、Java、IOS
其中Java和IOS继承自Language
Language和Persion都继承自NSObject


#import 
#import "Language.h"
#import "IOS.h"
#import "Java.h"
//模仿NSMutableArray系统自带
@interface Person : NSObject
//语言
//@property(nonatomic) id language;
//@property(nonatomic) IOS *language;
@property(nonatomic) ObjectType language;
/*
 id: 任何对象都能传进来
 Language: 在外面调用的时候,没有提示
 IOS:外面调用只能传对象
 */
 
 
//如果没有 泛型就确定,就是id类型
Person *p  = [[Person alloc] init];
p.language = [IOS new];
@end

泛型 协变和异变

协变

__covariant(协变):用于数据强制类型,可以向上强转,子类可以转成父类(NSArray点进去可以看到修饰符和NSMutableArray不一样)

只需要在Persion中的ObjectType前面加上__covariant
@interface Person<__covariant ObjectType> : NSObject

然后在ViewController中调用的时候就可以使用了
Person *p = [Person new];
    
Person *iosP  = [[Person alloc] init];
iosP.language = [IOS new];

//如果子类想给父类赋值,协变
p = iosP;
逆变

__contravariant(逆变):用于泛型类型数据强制类型,可以向下强转,父类可以转成子类

和协变类似,Persion后的修饰词改成__contravariant就是逆变
@interface Person<__contravariant ObjectType> : NSObject

Person *p = [Person new];
Person *iosP  = [[Person alloc] init];
iosP.language = [IOS new];
//逆变
iosP = p;

3.__kindof

点开UITableView里面,发现里面有__kindof的关键字

__kindof:表示当前类或者它子类
__kindof书写格式:
放在类型前面,表示修饰这个类型
__kindof:在调用的时候,很清楚的知道返回的类型


同样创建一个继承自NSObject的Person类,Son类继承自这个类
在Person类中写方法

法1
.h
+(id)person;
.m
+(id)person{
    return [[self alloc] init];
}

法2
仅仅表示Person类
.h
+(Person *)person;
.m
+(Person *)person{
    return [[self alloc] init];
}

法3
会自动识别当前类的调用
.h
+(instancetype)person;
.m
+(instancetype)person{
    return [[self alloc] init];
}

法4
__kindof Person * 表示可以是Person类或者他的子类
.h
+(__kindof Person *)person;
.m
+(Person *)person{
    return [[self alloc] init];
}

在ViewController中调用的时候,可以清晰的看出来[Son person]定义的对象的类型,可以是Person,也可以是Son类型的
Son *s = [Son person];
id的坏处:1.不能在编译的时候检测真是类型  不能调用.点语法
         2.返回值没有提示
NSString *str = [Son person];

你可能感兴趣的:(iOS9新特性)