以前的编程经验告诉我们判断一个对象是否为空或者是否实例化可以这样

if(someObject == nil){
  //do something
}else{
}

但是在Swift中这样是不行的,然后我在Stackoverflow上找到了答案

http://stackoverflow.com/questions/24314646/swift-with-nil

var s: String? = nil
s === nil // true

The only caveat is that to compare to nil, your variable must able to be nil. This means that it must be an optional, denoted with a ?.

var s: String is not allowed to be nil, so would therefore always returns false when ===compared to nil.

这里提到,如果想对象可能是nil,你的对象必须是optional的。

The documentation states:

=== or “Identical to” means that two constants or variables of class type refer to exactly the same class instance.

== or “Equal to” means that two instances are considered “equal” or “equivalent” in value, for some appropriate meaning of “equal”, as defined by the type’s designer.”

In Swift, there are class types (class) and value types (structs, enums). Int is not a class type, it's a value type. === operator is defined only for class types (and also for Array but that's a special case).

swift someObject == nil 如何实现的更多相关文章

  1. Swift - 11 - nil聚合运算

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  2. IOS系列swift语言之课时四

    今天我们要讲的主要有:下标.可为空的类型.枚举(特殊的枚举:递归枚举).原生值.关联值 首先来分析一下这个下标,就是说我们可以通过下标找到对应的值或者是改变对应的值. 其次是可为空的类型,我们要牢记所 ...

  3. iOS - nil null Nil笔记

    今天查看Nullability and Objective-C发现里面提到了nil和Null等关键字,做一下笔记. nil  ->   Null-Pointer to Objective-C O ...

  4. iOS 关于nil和Nil及null与<null>的区别

    问题是这样的. NSDictionary *sample = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadin ...

  5. Swift学习笔记

    swift 面向过程 数据结构 3.1 常量和变量 定义常量和变量 let a = 1 var b = 2 显式定义和隐式定义 无需指定强类型,编译器会自动根据初始值推断出其类型.与c#相似.如果在定 ...

  6. Objective-C 中 NULL、nil、Nil、NSNull 的定义及不同

    本文由我们团队的 康祖彬 童鞋撰写,这是他的个人主页:https://kangzubin.cn. 理解"不存在"的概念不仅仅是一个哲学的问题,也是一个实际的问题.我们是有形宇宙的居 ...

  7. iOS中nil、Nil、NULL、NSNull详解(转)

    ObjC 里面的几个空值符号经常会差点把我搞死,这些基础的东西一点要弄清楚才行,以提高码农的基本素质. nil nil 是 ObjC 对象的字面空值,对应 id 类型的对象,或者使用 @interfa ...

  8. Swift 可选值(Optional Values)介绍

    Optional的定义 Optional也是Objective-C没有的数据类型,是苹果引入到Swift语言中的全新类型,它的特点就和它的名字一样:可以有值,也可以没有值,当它没有值时,就是nil.此 ...

  9. 在ios开发中nil和NUll和Nilde区别————和如何判断连个对象的关系和UISlider不能拖动的问题

    nil表示一个对象指针为空,针对对象 >示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = nil; ...

随机推荐

  1. 关于oracle中传过来的一个多id需要插入到数据库用,分格的存储过程

    create or replace procedure test ( jf_Id in nvarchar2, yf_id in nvarchar2 ) as v_length NUMBER := LE ...

  2. WWF3状态机工作流<WWF第七篇>

    状态机是另外一种常见的工作流类型.它是以状态的变迁为驱动而进行业务流转的,是一定需要人为干预的,而不像顺序类型工作流那样可以按照事先设计好的业务流程一步一步依次执行下去. 一.状态机工作流范例 Sta ...

  3. javascript 同步加载与异步加载

    HTML 4.01 的script属性 charset: 可选.指定src引入代码的字符集,大多数浏览器忽略该值. defer: boolean, 可选.延迟脚本执行,相当于将script标签放入页面 ...

  4. leetcode 110

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  5. hdu2078

    刚开始看这题,感觉是DP什么的 ,后来我发现,只要找到中最小值,就可以啦,哈哈.假如用x1把0-100分割. 则0-x1-100  ===>   x1^2+(100-x1)^2 跟0-100   ...

  6. SQLserver2012 修改数据库架构

    还原数据库以后,发现有一张表的架构不对,执行sql提示:对象名无效.

  7. Ubuntu 14.04 – How to install xrdp in Ubuntu 14.04

    http://c-nergy.be/blog/?p=5305 Hello World, Ubuntu 14.04 has been released on April 17th 2014 and we ...

  8. Knockout.Js官网学习(监控属性Observables)

    前言 1.创建一个ViewModel <script type="text/javascript"> //1.创建一个ViewModel var myViewModel ...

  9. C# MongoDB--时区问题(差了8小时)

    原因:MongoDB中存储的时间是标准时间UTC +0:00C#的驱动支持一个特性,将实体的时间属性上添加上这个特性并指时区就可以了.例如:[BsonDateTimeOptions(Kind = Da ...

  10. Enum枚举类型的使用笔记

    好处: 1.可以直接使用switch 2.可以实现toString()方法 笔记: 1.枚举类头部定义的成员变量,可以看做是枚举类的一个实例 public enum Color { RED(" ...