• iOS开发新技术篇—iOS9新特性


    一.关键字
     iOS9新特性之关键字
     * iOS9新出的关键字:用来修饰属性,或者方法的参数,方法的返回值
     * 好处:
    • 1.迎合swift
    • 2.提高我们开发人员开发规范,减少程序员之间交流
     
     * 注意:
     * iOS9新出关键字nonnull,nullable,null_resettable,_Null_unspecified只能"修饰对象,不能修饰基本数据类型"
    1.nullable作用:表示可以为空
          nullable更多的作用在于程序员之间的沟通交流(提醒同事某个属性可能是nil)
          默认情况下, 不加nullable, setter 和 getter 都是可以为nil
          nullable __nullable : setter 和 getter 都可以为nil
    书写格式:
     // 方式一:
     @property (nonatomic, strong, nullable) NSString *name;
     // 方式二:
     @property (nonatomic, strong) NSString *_Nullable name;
     // 方式三:
     @property (nonatomic, strong) NSString *__nullable name;
     
    2.nonnull: non:非 null:空
          nonnull __nonnull : setter 和 getter 都不能为nil
    书写格式:
     // 方式一:
     @property (nonatomic, strong, nonnull) NSString *icon;
     // 方式二:
     @property (nonatomic, strong) NSString * _Nonnull icon;
     // 方式三:
     @property (nonatomic, strong) NSString * __nonnull icon;
     
    在NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END之间,定义的所有对象属性和方法默认都是nonnull
    // 方法中,关键字书写规范
    // 方式一:
    - (nonnull NSString *)test:(nonnull NSString *)str;
    // 方式二:
    - (NSString * _Nonnull)test:(NSString * _Nonnull)str;
     
    3.null_resettable:  setter可以为nil, getter不可以为nil
     // 注意;如果使用null_resettable,必须 重写get方法或者set方法,处理传递的值为空的情况
     // 书写方式:
     @property (nonatomic, strong, null_resettable) NSString *name;
     
    4. _Null_unspecified:不确定是否为空
        书写方式只有这种
      
    // 方式一:
        @property (nonatomic, strong) NSString *_Null_unspecified name;
       // 方式二:
        @property (nonatomic, strong) NSString *__null_unspecified name;
     
    二、泛型
    泛型:限制类型

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

    泛型书写规范
    在类型后面定义泛型,NSMutableArray<UITouch *> *datas

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

    泛型好处:
    1.提高开发规范,减少程序员之间交流
    2.通过集合取出来对象,直接当做泛型对象使用,可以直接使用点语法
     
    #import <Foundation/Foundation.h>
    #import "Language.h"
     
    // 声明泛型
    @interface Person<__contravariant ObjectType> : NSObject
    
    // 语言
    @property (nonatomic)  ObjectType language;
    
    /*
     id:任何对象都能传进来
     Language:在外面调用的时候,没有提示
     iOS* 以后只能传对象
     */
    - (ObjectType)language;
    - (void)setLanguage:(ObjectType)language;
    
    @end
    泛型没有确定,就是id类型
     
    三、协变__covariant/逆变__contravariant
        __covariant(协变):用于泛型数据强转类型,可以向上强转,子类 可以转成 父类
         // __covariant : 小类型(泛型类的子类类型) -> 大类型(泛型类的父类类型)  
       __contravariant(逆变):用于泛型数据强转类型,可以向下强转, 父类 可以 转成子类
         // __contravariant : 大类型(泛型类的父类类型) -> 小类型(泛型类的子类类型)
    书写规范
    // 声明协变性的泛型
    @interface Person<__covariant ObjectType> : NSObject
    // 声明逆变性的泛型
    @interface Person<__contravariant ObjectType> : NSObject
    四、__kindof
    __kindof:表示当前类或者它子类

    __kindof书写格式:
    放在类型前面,表示修饰这个类型(__kindof Person *)
    __kindof  :在调用的时候,很清楚的知道返回类型
      id坏处:  1.不能在编译的时候检查真实类型
               2.返回值,没有提示
     
    三种方式对比结果:
    #import <Foundation/Foundation.h>
     
    @interface Person : NSObject
    // 仅仅表示只能是Person类
    + (Person *)person1;
     
    // 会自动识别当前对象的类
    + (instancetype)person;
    
    // __kindof Person *:表示可以是Person类或者它的子类
    + (__kindof Person *)person;
     
    @end
  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/zengshuilin/p/5776932.html
Copyright © 2020-2023  润新知