ios swift 语言中的 null / nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24043589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
null / nil in swift language
提问by Mehul Parmar
How would we define the following in swift programming language :
我们将如何在 swift 编程语言中定义以下内容:
- null
- nil
- Nil
- [NSNull null]
- 空值
- 零
- 零
- [NSNull 空]
In other words, what would be the swift equivalent of each of these objective c terms. Besides, would also like to know whether any specific use cases exist for non objective c types like structs and enums. Thanks in advance.
换句话说,这些客观 c 项的 swift 等价物是什么。此外,还想知道对于非目标 c 类型(如结构和枚举)是否存在任何特定用例。提前致谢。
回答by gwcoffey
Regarding equivalents:
关于等价物:
NULL
has no equivalent in Swift.nil
is also callednil
in SwiftNil
has no equivalent in Swift[NSNull null]
can be accessed in Swift as NSNull()
NULL
在 Swift 中没有等价物。nil
nil
在 Swift 中也被调用Nil
在 Swift 中没有等价物[NSNull null]
可以在 Swift 中作为 NSNull() 访问
Note: These are my guesses based on reading and play. Corrections welcome.
注意:这些是我根据阅读和游戏的猜测。欢迎指正。
But nil/NULL handling in Swift is very different from Objective C. It looks designed to enforce safety and care. Read up on optionals in the manual. Generally speaking a variable can't be NULL at all and when you need to represent the "absence of a value" you do so declaratively.
但是 Swift 中的 nil/NULL 处理与 Objective C 非常不同。它看起来旨在加强安全性和谨慎性。阅读手册中的选项。一般来说,变量根本不能为 NULL,当您需要表示“缺少值”时,您可以声明性地这样做。
回答by Rien
If you need to use a NULL at low level pointer operations, use the following:
如果您需要在低级指针操作中使用 NULL,请使用以下命令:
UnsafePointer<Int8>.null()
回答by Tommy
nil
means "no value" but is completely distinct in every other sense from Objective-C's nil
.
nil
意味着“没有价值”,但在所有其他意义上都与 Objective-C 的nil
.
It is assignable only to optional variables. It works with both literals and structs (i.e. it works with stack-based items, not just heap-based items).
它只能分配给可选变量。它适用于文字和结构(即它适用于基于堆栈的项目,而不仅仅是基于堆的项目)。
Non-optional variables cannot be assigned nil
even if they're classes (i.e. they live on the heap).
nil
即使它们是类(即它们位于堆上),也不能分配非可选变量。
So it's explicitly not a NULL
pointer and not similar to one. It shares the name because it is intended to be used for the same semantic reason.
所以它明确不是一个NULL
指针,也不类似于一个。它共享名称,因为它旨在用于相同的语义原因。
回答by ChandreshKanetiya
Swift's nil
is not the same as nil
in Objective-C.
In Objective-C, nil
is a pointer to a non-existent object. In Swift, nil
is not a pointer—it is the absence of a value of a certain type. Optionals of any type can be set to nil
, not just object types.
Swiftnil
与nil
Objective-C 不同。
在 Objective-C 中,nil
是指向不存在对象的指针。在 Swift 中,nil
不是指针——它是某种类型的值的缺失。任何类型的可选项都可以设置为nil
,而不仅仅是对象类型。
NULL
has no equivalent in Swift.
NULL
在 Swift 中没有等价物。
nil
is also called nil in Swift
nil
在 Swift 中也称为 nil
Nil
has no equivalent in Swift
Nil
在 Swift 中没有等价物
[NSNull null]
can be accessed in Swift as NSNull()
[NSNull null]
可以在 Swift 中访问为 NSNull()
回答by Ciprian
The concept of Null in Swift resumes to the Optional enum. The enum is defined like this
Swift 中 Null 的概念恢复到 Optional 枚举。枚举是这样定义的
enum Optional<T> {
case Some(T)
case None
}
What this means is that you cannot have an uninitialised variable/constant in Swift.. If you try to do this, you will get a compiler error saying that the variable/constant cannot be uninitialised. You will need to wrap it in the Optional enum..
这意味着您不能在 Swift 中使用未初始化的变量/常量。您需要将其包装在 Optional 枚举中。
This is the only Null concept you will find in Swift
这是你在 Swift 中唯一能找到的 Null 概念
回答by Markus
I had this problem in a problematic scenario: the Settings Bundle
我在一个有问题的场景中遇到了这个问题:设置包
So that someone does not waste his time (As I lost it!)
以免有人浪费他的时间(因为我失去了它!)
let userDefaults = UserDefaults.standard
var settingsValue = userDefaults.string(forKey: "user_preference")
if settingsValue == nil {
settingsValue = "1"
}
if (settingsValue?.isEqual("1"))! {
//Do something
} else {
//Do something
}
Until the user open the App settings for first time, the system returns nil. And, in my case, the App crashes.
直到用户第一次打开App设置,系统返回nil。而且,就我而言,应用程序崩溃了。
回答by Yakup Ad
if !(myDATA is NSNull) {
// optional is NOT NULL, neither NIL nor NSNull
} else {
// null
}