美文网首页iOS-Objective-C
OC中的一些关键字

OC中的一些关键字

作者: 追沐 | 来源:发表于2017-07-24 14:56 被阅读15次

nullable/_Nullable/__nullable

  • nullable修饰属性,表示属性可为空,可将属值赋为nil。

  • 声明一个属性值是可为空的,有如下三种方式:

@property (nonatomic, strong, nullable) NSString   *nullableString;
@property (nonatomic, strong          ) NSString * _Nullable  nullableString1;
@property (nonatomic, strong          ) NSString * __nullable nullableString2;

nonnull/_Nonnull/__nonnull

  • nonnull修饰属性,表示该属性不可为空,不能将属性值赋nil,赋nil系统就会报警高。
  • 同样的声明一个属性不可为空也同理。

__kindof

  • __kindof修饰,表示是该类或者该类的子类
//声明一个数组,该数组里只能是字符串类型的数据
NSArray <NSString *> *stringArray = @[@"字符串1",@"字符串2"]; 

__kindof被广泛使用在数组、字典等,例如:

//声明一个数组,该数组中只能是,是UIGestureRecognizer类或者其子类对象
@property(nonatomic, readonly) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers;

例如系统tableView方法

- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;  

- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;

- (nullable __kindof UITableViewHeaderFooterView *)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier;  

instancetype 能自动识别当前类的对象,id不能

  • id是一个指向OC对象的指针。
  • instancetype指向的是当前类的对象。

例如:

//返回当前类的一个对象
- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

相关文章

  • oc的基本语法,类和对象

    1. 关键字 OC中的关键字基本上所有的都是以@开头,但也有少数不是以@开头的,下面是一些较常用的关键字: @in...

  • OC中的一些关键字

    nullable/_Nullable/__nullable nullable修饰属性,表示属性可为空,可将属值赋为...

  • OC中的一些关键字

    static、self 、super static: 1、static可以修饰OC中所有的成员变量,有局部和全局之...

  • 【知识总结】OC中的继承

    关键字 OC中也可以使用上面的关键字设置属性的权限。在OC中,默认情况下,在头文件中实现的是public,在m文件...

  • OC中的一些修饰关键字

    nullable: 表示对象可以为空下面是三种写法: nonnull: 表示对象不能为空下面是三种写法: null...

  • Core Data

    // Core Data // OC 中的 @dynamic 和 Swfit 中的 dynamic 关键字作用不同...

  • swift中的case where精准匹配

    在oc中,case关键字出现在switch流程控制语法中,swift也不例外.但是相较与oc中switch只能筛选...

  • OC中关键字

    static: 1、static可以修饰OC中所有的成员变量,有局部和全局之分;static还可以修饰方法 2、s...

  • OC中的关键字

    参考了:某大神 目录:关键字const/static/extern的意思&&用法 a、const; b、stati...

  • OC中的关键字

    原文链接:https://www.cnblogs.com/caishangshu/articles/ioskeys...

网友评论

    本文标题:OC中的一些关键字

    本文链接:https://www.haomeiwen.com/subject/ajavkxtx.html