swift someObject == nil 如何实现
以前的编程经验告诉我们判断一个对象是否为空或者是否实例化可以这样
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).
Intis not a class type, it's a value type.===operator is defined only for class types (and also forArraybut that's a special case).
swift someObject == nil 如何实现的更多相关文章
- Swift - 11 - nil聚合运算
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- IOS系列swift语言之课时四
今天我们要讲的主要有:下标.可为空的类型.枚举(特殊的枚举:递归枚举).原生值.关联值 首先来分析一下这个下标,就是说我们可以通过下标找到对应的值或者是改变对应的值. 其次是可为空的类型,我们要牢记所 ...
- iOS - nil null Nil笔记
今天查看Nullability and Objective-C发现里面提到了nil和Null等关键字,做一下笔记. nil -> Null-Pointer to Objective-C O ...
- iOS 关于nil和Nil及null与<null>的区别
问题是这样的. NSDictionary *sample = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadin ...
- Swift学习笔记
swift 面向过程 数据结构 3.1 常量和变量 定义常量和变量 let a = 1 var b = 2 显式定义和隐式定义 无需指定强类型,编译器会自动根据初始值推断出其类型.与c#相似.如果在定 ...
- Objective-C 中 NULL、nil、Nil、NSNull 的定义及不同
本文由我们团队的 康祖彬 童鞋撰写,这是他的个人主页:https://kangzubin.cn. 理解"不存在"的概念不仅仅是一个哲学的问题,也是一个实际的问题.我们是有形宇宙的居 ...
- iOS中nil、Nil、NULL、NSNull详解(转)
ObjC 里面的几个空值符号经常会差点把我搞死,这些基础的东西一点要弄清楚才行,以提高码农的基本素质. nil nil 是 ObjC 对象的字面空值,对应 id 类型的对象,或者使用 @interfa ...
- Swift 可选值(Optional Values)介绍
Optional的定义 Optional也是Objective-C没有的数据类型,是苹果引入到Swift语言中的全新类型,它的特点就和它的名字一样:可以有值,也可以没有值,当它没有值时,就是nil.此 ...
- 在ios开发中nil和NUll和Nilde区别————和如何判断连个对象的关系和UISlider不能拖动的问题
nil表示一个对象指针为空,针对对象 >示例代码: NSString *someString = nil; NSURL *someURL = nil; id someObject = nil; ...
随机推荐
- J2SE宏观总结
- MongoDB 3: 使用中的问题,及其应用场景
导读:用MongoDB去存储非关系型的数据,是一个比较正确的选择.但是,如果只是用MongoDB,那么也会出现一些问题.MongoDB,尤其使用的最佳场景,更多的时候,需要结合关系型数据库共同解决问题 ...
- Secure your iPhone with 6 digit passcode by upgrading to iOS9
IP-Box could crack 4 digit passcode, what about 6 digit passcode??? All you need to do is to upgrade ...
- TP-Link访问策略
参考资料: http://service.tp-link.com.cn/detail_article_111.html
- Android IOS WebRTC 音视频开发总结(七)-- 基于浏览器的开发
前面写的一系列总结都是讲webrtc如何下载,编译,开发的,有些人可能有点云里雾里了,WEBRTC不是用来搞跨浏览器开发的吗,怎么我讲的这些跟浏览器扯不上任何关系,其实看看下面这个架构图,你就明白了, ...
- MySQL允许远程访问
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option; flush privileges; ...
- 为什么swing不适合做桌面软件
http://www.zhihu.com/question/19608871 我最近几年做的项目清一色的都是HTML5了,这篇<基于HTML5的电信网管3D机房监控应用>供参考,HTML5 ...
- WCF学习笔记(一)
WCF是什么? 官方解释: Windows Communication Foundation (WCF) 是用于构建面向服务的应用程序的框架.借助 WCF,可以将数据作为异步消息从一个服务终结点发送至 ...
- Overview Of Portal Registry And Content References
Portal Registry Each portal is defined by a portal registry.A portal registry has a tree-like struc ...
- Yii整合ZF2及soap实例
一)如何整合? // change the following paths if necessary $yii = dirname(__FILE__).'/framework/yii.php'; $c ...