zoukankan      html  css  js  c++  java
  • NULL vs nil in ObjectiveC

      nil只能被应用在id类型的地方,就是Java与C++中指向对象的指针。而NULL用于非对象指针。

    请看如下实例:

     -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object

    change:(NSDictionary *)change context:(void *)context
      形参context是空指针类型,是一个C类型指针,所以用NULL定义,空指针类型有时被声明为
    (void *)0 ,而不是 id类型的nil.

    原文:

    "nil" should only be used in place of an "id", what we Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers.

    Look at the declaration of that method:

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
    change
    :(NSDictionary *)change context:(void *)context

    context is a "void *" (ie a C-style pointer), so you'd definitely use NULL (which is sometimes declared as "(void *)0") rather than nil (which is of type "id").

      

  • 相关阅读:
    对js数组的splice实现
    前端必读
    命令模式
    访问者模式
    观察者模式
    解释器模式
    装饰器模式
    组合模式
    抽象工厂模式
    搜索结果关键词高亮显示
  • 原文地址:https://www.cnblogs.com/mac_arthur/p/1718328.html
Copyright © 2011-2022 走看看