Working with nil
【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的更多相关文章
- Scala中None, Nil, Nothing的区别
Nil是一个空的List None是一个object,是Option的子类型 List[Nothing]
- undefined method `environment' for nil:NilClass when importing Bootstrap into rails
今天做项目时往Gemfile里加了各gem, 然后bundle update了一下, 然后悲剧了,出现了undefined method `environment' for nil:NilClass ...
- 解决NSData转NSString返回nil的问题
// 字符串转Data NSString *str =@"jesfds"; NSData *data =[str dataUsingEncoding:NSUTF8StringEnc ...
- "undefined method `root' for nil:NilClass" error when using "pod install" 解决办法
如果pod undate 的时候报错"undefined method `root' for nil:NilClass" error when using "pod in ...
- nil、Nil、NULL、NSNull的区别
nil:指向一个对象的空指针,对objective c id 对象赋空值. Nil:指向一个类的空指针,表示对类进行赋空值. NULL:指向其他类型(如:基本类型.C类型)的空指针, 用于对非对象指针 ...
- -[NSBundle initWithURL:]: nil URL argument'
今天早上同事突然跟我说趣拍的SDK不能用了,一调用就crash,我一听就纳了闷了,原来好好的啊. 然后就开始查呗,马上就要上线了,不搞好,老大会不会杀了我... 搞个全局断点,就停在了一堆我看不懂的界 ...
- iOS 开发遇到的问题之(nil指针对NSDictionary及NSArray初始化的影响)
nil指针对NSDictionary及NSArray初始化的影响 最近在做项目的时候遇到一个挺坑的崩溃问题,是由于NSDictionary初始化时nil指针引起的崩溃.假设我们现在要初始化一个{key ...
- 类和对象 nil/Nil/NULL的区别
iOS-----类和对象,nil/Nil/NULL的区别 iOS中类和对象,nil/Nil/NULL的区别 类与对象的概念 类是对同一类事物高度的抽象,类中定义了这一类对象所应具有的静态属性(属性 ...
- Objective-C数据类型之id,SEL,BOOL,nil,NULL和NSNull
id id是指向Objective-C对象的指针,等价于C语言中的void*,可以映射任何对象指针指向他,或者映射它指向其他的对象.常见的id类型就是类的delegate属性. SEL SEL类型是 ...
- Rails :.nil? , .empty?, .blank? .present? 的区别
.nil? , .empty?, .blank? .present? 的区别 首先这三个都是判空的. 而 .nil? 和 .empty? 是ruby的方法. .blank? 是rails的方法 .ni ...
随机推荐
- cocos2dx Android 环境搭建 以及 ndk调试
最近在学习cocos2dx,真的很强大,使我们更专注于游戏趣味,免去了繁琐的底层框架代码. cocos2dx的最强大之处当然在于跨平台.跨平台首选当然是Android,好记性不如烂笔头,记下本文分享给 ...
- sgu 495. Kids and Prizes (简单概率dp 正推求期望)
题目链接 495. Kids and Prizes Time limit per test: 0.25 second(s)Memory limit: 262144 kilobytes input: s ...
- 【笨嘴拙舌WINDOWS】GDI映射方式
TextOut(hdc,100,100,TEXT(“Love China”),10) 这句GDI函数的作用是在坐标点(100,100)的位置输出一个“Love China”字符串: GDI函数作为硬件 ...
- MVC 中使用扩展方法
扩展方法(Extension Method)是给那些不是你拥有.因而不能直接修改的类添加方法的一种方便的办法. 一.使用扩展方法 1.定义一个购物车的类-ShoppingCart using Sys ...
- (转载)NSTimer
转自:http://www.cnblogs.com/smileEvday/archive/2012/12/21/NSTimer.html 看到这个标题,你可能会想NSTimer不就是计时器吗,谁不会用 ...
- 类的加载到反射reflect
类的加载: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化这三个步骤来实现对这个类进行初始化. 加载: 就是指将class文件加载进入内存,并为之创建一个Class对 ...
- 学习java之HashMap和TreeMap
HashMap和TreeMap是Map接口的两种实现,ArrayDeque和LinkedList是Queue接口的两种实现方式.下面的代码是我今天学习这四个数据结构之后写的.还是不熟悉,TreeMap ...
- (三)spark集群DHCP IP变化后的处理
学校的网比较特殊(本人比较菜),需要DHCP获得地址,那么有时候IP被占用了,应该如何应对呢? 1)修改/etc/hosts 2) 修改spark-env.sh 里的 Master 地址 ,Maste ...
- C#利用最新版的WPS实现导入导出
微软的EXCEl操作相信大家也知道,不方便,安装包太大,而且表格的数据量也只有6000多(是6000多还是60000多我就忘记了),在导出导入大量数据的就没办法,而wsp表格则实现了百万数据的容量,而 ...
- myeclipse 8.5最新注册码(过期时间到2016年)
myeclipse 8.5最新注册码(过期时间到2016年) 这几天myeclipse弹出注册码过期,去网上一搜,要么已过期,要么就剩一两个月.倒腾了半天,自己申请了几个注册码,给大家分享一下 Sub ...