Working with nil

  It’s always a good idea to initialize scalar variables at the time you declare them, otherwise their initial values will contain garbage from the previous stack contents:

  

  This isn’t necessary for object pointers, because the compiler will automatically set the variable to nil if you don’t specify any other initial value:

  

  A nil value is the safest way to initialize an object pointer if you don’t have another value to use, because it’s perfectly acceptable in Objective-C to send a message to nil. If you do send a message to nil, obviously nothing happens.

  Note: If you expect a return value from a message sent to nil, the return value will be nil for object return types, 0 for numeric types, and NO for BOOL types. Returned structures have all members initialized to zero.

  If you need to check to make sure an object is not nil (that a variable points to an object in memory), you can either use the standard C inequality operator:

  

  or simply supply the variable:

  

  If the somePerson variable is nil, its logical value is 0 (false). If it has an address, it’s not zero, so evaluates as true.

Similarly, if you need to check for a nil variable, you can either use the equality operator:

  

  or just use the C logical negation operator:

  

Working with nil的更多相关文章

  1. Scala中None, Nil, Nothing的区别

    Nil是一个空的List None是一个object,是Option的子类型 List[Nothing]

  2. undefined method `environment' for nil:NilClass when importing Bootstrap into rails

    今天做项目时往Gemfile里加了各gem, 然后bundle update了一下, 然后悲剧了,出现了undefined method `environment' for nil:NilClass ...

  3. 解决NSData转NSString返回nil的问题

    // 字符串转Data NSString *str =@"jesfds"; NSData *data =[str dataUsingEncoding:NSUTF8StringEnc ...

  4. "undefined method `root' for nil:NilClass" error when using "pod install" 解决办法

    如果pod undate 的时候报错"undefined method `root' for nil:NilClass" error when using "pod in ...

  5. nil、Nil、NULL、NSNull的区别

    nil:指向一个对象的空指针,对objective c id 对象赋空值. Nil:指向一个类的空指针,表示对类进行赋空值. NULL:指向其他类型(如:基本类型.C类型)的空指针, 用于对非对象指针 ...

  6. -[NSBundle initWithURL:]: nil URL argument'

    今天早上同事突然跟我说趣拍的SDK不能用了,一调用就crash,我一听就纳了闷了,原来好好的啊. 然后就开始查呗,马上就要上线了,不搞好,老大会不会杀了我... 搞个全局断点,就停在了一堆我看不懂的界 ...

  7. iOS 开发遇到的问题之(nil指针对NSDictionary及NSArray初始化的影响)

    nil指针对NSDictionary及NSArray初始化的影响 最近在做项目的时候遇到一个挺坑的崩溃问题,是由于NSDictionary初始化时nil指针引起的崩溃.假设我们现在要初始化一个{key ...

  8. 类和对象 nil/Nil/NULL的区别

    iOS-----类和对象,nil/Nil/NULL的区别   iOS中类和对象,nil/Nil/NULL的区别 类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性 ...

  9. Objective-C数据类型之id,SEL,BOOL,nil,NULL和NSNull

     id id是指向Objective-C对象的指针,等价于C语言中的void*,可以映射任何对象指针指向他,或者映射它指向其他的对象.常见的id类型就是类的delegate属性. SEL SEL类型是 ...

  10. Rails :.nil? , .empty?, .blank? .present? 的区别

    .nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...

随机推荐

  1. 漫游Kafka设计篇之消息传输的事务定义

    之前讨论了consumer和producer是怎么工作的,现在来讨论一下数据传输方面.数据传输的事务定义通常有以下三种级别: 最多一次: 消息不会被重复发送,最多被传输一次,但也有可能一次不传输. 最 ...

  2. iOS 8 AutoLayOut入门

    http://blog.csdn.net/asdfg13697116596/article/details/42562565 iOS 8 AutoLayOut入门自从iOS6带来Auto Layout ...

  3. HDU5463 Clarke and minecraft

    解题思路:此题刚开始,觉得好繁琐,好混乱,理清思路后,发现很简单.   具体见代码分析. #include<cstdio> #include<cstring> #include ...

  4. 【英语】Bingo口语笔记(77) - 临时改变计划的表达

  5. Comparator与Comparable的异同

    同 它们都是Java的一个接口,都是用来实现集合中元素的比较.排序. 异 Comparator位于java.util下: Comparable位于java.lang下: Comparable(可比较的 ...

  6. Windows下配置cygwin和ndk编译环境

    cygwin安装 正确的安装步骤其实很简单:1. 下载setup-86_64.exe 2. 直接从网上下载安装,选择包时,顶部选择“default”不变 3. 搜索make,勾选make,cmake, ...

  7. Android 注解的一些应用以及原理

    在这边文章之前你首先需要对java 的注解部分有一个基本的了解(不需要太过的深入). 简单来说,注解这个东西就是用于辅助我们开发java代码的,注解本身无法干扰java源代码的执行. 在android ...

  8. c++ 一个类使用另外一个类的变量或方法

    如:a.cpp 声明 int a=9; 要在b.cpp文件中使用变量 a extern int a; int b=1; cout<<a+b; 结果为10;

  9. hdu 2897(威佐夫博奕变形)

    题意:容易理解. 分析:当n%(p+q)==0时,先取者必胜,必胜方案:先取q,然后对方去x个,先取者就取(p+q-x)个,最后对方就必须取玩p个, 当n%(p+q)==r(r<=p),先取者必 ...

  10. 使用命令行设置svn忽略列表

    Windows 上的 TortoiseSVN 设置 svn 的忽略列表是非常方便的,但是在Mac OS X上,好用的图形化 svn 客户端都有点儿贵,比如 Versions 和 CornerStone ...