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. CSS控制超链接

    一.伪类 CSS控制元素的某种状态---偽类(用于向某些选择器添加特殊的效果)    偽类的语法:元素标签 偽类名称{属性:属性值;} 二.超链接        a:link:未访问的链接       ...

  2. R语言 rwordseg包的下载

    在CRAN中没有,如果通过R下载经常会出错,使用以下地址下载后加载本地包 http://R-Forge.R-project.org/bin/windows/contrib/3.0/Rwordseg_0 ...

  3. 如何在Android studio中同时打开多个工程? (转载)

    最近学习Android Studio,想同时打开两个Project.但是点击File->Open之后,原有的Project被关闭掉了.怎么在新的窗口中打开Project呢? 解决: 点击Help ...

  4. linux系统查看磁盘分区使用空间

    Df命令是linux系统以磁盘分区使用空间:df -hl

  5. LeetCode Reverse Linked List II 反置链表2

    题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...

  6. ecshop 首页调用指定类产品

    方法一.已测试成功 1.在/includes/lib_goods.php最底部增加以下代码 function index_get_cat_id_goods_best_list($cat_id = '' ...

  7. 广义线性模型 GLM

    Logistic Regression 同 Liner Regression 均属于广义线性模型,Liner Regression 假设 $y|x ; \theta$ 服从 Gaussian 分布,而 ...

  8. Oracle 单实例 迁移到 RAC 实例 -- 使用RMAN 异机恢复

    Oracle 官网有关单实例迁移到RAC的一个步骤说明: How to Convert 10g Single-Instance database to 10g RAC using Manual Con ...

  9. Quartz.net 2.0的使用说明

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于WinForm和ASP.NET应用中.它提供了巨大的灵活性而不牺牲 ...

  10. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...