How do you properly unwrap both normal and implicit optionals?
你如何正确解开正常和隐含的期权?
There seems to be confusion in this topic and I would just like to have a reference for all of the ways and how they are useful.
这个主题似乎有些混乱,我想对所有方法以及它们如何有用提供参考。
There are currently two ways to create optionals:
目前有两种创建选项的方法:
var optionalString: String?
var implicitOptionalString: String!
What are all the ways to unwrap both? Also, what is the difference between using !
and ?
during the unwrapping?
解开两者的方法是什么?另外,使用有什么区别!和?打开期间?
3 个解决方案
#1
32
There are many similarities and just a handful of differences.
有许多相似之处,只有少数差异。
(Regular) Optionals
-
Declaration:
var opt: Type?
声明:var opt:Type?
-
Unsafely unwrapping:
let x = opt!.property // error if opt is nil
不安全展开:如果opt为零,则让x = opt!.property //错误
-
Safely testing existence :
if opt != nil { ... someFunc(opt!) ... } // no error
安全测试存在:if opt!= nil {... someFunc(opt!)...} //没有错误
-
Safely unwrapping via binding:
if let x = opt { ... someFunc(x) ... } // no error
通过绑定安全地解包:如果让x = opt {... someFunc(x)...} //没有错误
-
Safely chaining:
var x = opt?.property // x is also Optional, by extension
安全链接:var x = opt?.property // x也是可选的,扩展名
-
Safely coalescing nil values:
var x = opt ?? nonOpt
安全地合并nil值:var x = opt ?? nonOpt
Implicitly Unwrapped Optionals
-
Declaration:
var opt: Type!
声明:var opt:类型!
-
Unsafely unwrapping (implicit):
let x = opt.property // error if opt is nil
不安全展开(隐式):如果opt为nil,则让x = opt.property //错误
-
Unsafely unwrapping via assignment:
let nonOpt: Type = opt // error if opt is nil
通过赋值不安全地展开:如果opt为nil,则让nonOpt:Type = opt //错误
-
Unsafely unwrapping via parameter passing:
func someFunc(nonOpt: Type) ... someFunc(opt) // error if opt is nil
通过参数传递不安全地展开:func someFunc(nonOpt:Type)... someFunc(opt)//如果opt为nil则出错
-
-
Safely testing existence:
if opt != nil { ... someFunc(opt) ... } // no error
安全地测试存在:如果选择!= nil {... someFunc(opt)...} //没有错误
-
Safely chaining:
var x = opt?.property // x is also Optional, by extension
安全链接:var x = opt?.property // x也是可选的,扩展名
-
Safely coalescing nil values:
var x = opt ?? nonOpt
安全地合并nil值:var x = opt ?? nonOpt
#2
5
Since Beta 5 we have also the new coalescing operator (??):
从Beta 5开始,我们还有了新的合并运算符(??):
var a : Int?
let b : Int = a ?? 0
If the optional is != nil it is unwrapped else the value on the right of the operator is used
如果可选项是!= nil则将其解包,否则将使用运算符右侧的值
#3
2
An optional type means that the variable might be nil.
可选类型意味着变量可能为零。
Example:
例:
var myString: Int? = 55
myString = nil
The question mark indicates it might have nil value.
问号表示它可能没有值。
But if you state like this:
但如果你这样陈述:
var myString : Int = 55
myString = nil
It will show error.
它会显示错误。
Now to retrieve the value you need to unwrap it:
现在要检索打开它所需的值:
print(myString!)
But if you want to unwrap automatically:
但是如果你想自动打开包装:
var myString: Int! = 55
Then:
然后:
print(myString)
No need to unwrap. Hope it will help.
无需打开包装。希望它会有所帮助。
#1
32
There are many similarities and just a handful of differences.
有许多相似之处,只有少数差异。
(Regular) Optionals
-
Declaration:
var opt: Type?
声明:var opt:Type?
-
Unsafely unwrapping:
let x = opt!.property // error if opt is nil
不安全展开:如果opt为零,则让x = opt!.property //错误
-
Safely testing existence :
if opt != nil { ... someFunc(opt!) ... } // no error
安全测试存在:if opt!= nil {... someFunc(opt!)...} //没有错误
-
Safely unwrapping via binding:
if let x = opt { ... someFunc(x) ... } // no error
通过绑定安全地解包:如果让x = opt {... someFunc(x)...} //没有错误
-
Safely chaining:
var x = opt?.property // x is also Optional, by extension
安全链接:var x = opt?.property // x也是可选的,扩展名
-
Safely coalescing nil values:
var x = opt ?? nonOpt
安全地合并nil值:var x = opt ?? nonOpt
Implicitly Unwrapped Optionals
-
Declaration:
var opt: Type!
声明:var opt:类型!
-
Unsafely unwrapping (implicit):
let x = opt.property // error if opt is nil
不安全展开(隐式):如果opt为nil,则让x = opt.property //错误
-
Unsafely unwrapping via assignment:
let nonOpt: Type = opt // error if opt is nil
通过赋值不安全地展开:如果opt为nil,则让nonOpt:Type = opt //错误
-
Unsafely unwrapping via parameter passing:
func someFunc(nonOpt: Type) ... someFunc(opt) // error if opt is nil
通过参数传递不安全地展开:func someFunc(nonOpt:Type)... someFunc(opt)//如果opt为nil则出错
-
-
Safely testing existence:
if opt != nil { ... someFunc(opt) ... } // no error
安全地测试存在:如果选择!= nil {... someFunc(opt)...} //没有错误
-
Safely chaining:
var x = opt?.property // x is also Optional, by extension
安全链接:var x = opt?.property // x也是可选的,扩展名
-
Safely coalescing nil values:
var x = opt ?? nonOpt
安全地合并nil值:var x = opt ?? nonOpt
#2
5
Since Beta 5 we have also the new coalescing operator (??):
从Beta 5开始,我们还有了新的合并运算符(??):
var a : Int?
let b : Int = a ?? 0
If the optional is != nil it is unwrapped else the value on the right of the operator is used
如果可选项是!= nil则将其解包,否则将使用运算符右侧的值
#3
2
An optional type means that the variable might be nil.
可选类型意味着变量可能为零。
Example:
例:
var myString: Int? = 55
myString = nil
The question mark indicates it might have nil value.
问号表示它可能没有值。
But if you state like this:
但如果你这样陈述:
var myString : Int = 55
myString = nil
It will show error.
它会显示错误。
Now to retrieve the value you need to unwrap it:
现在要检索打开它所需的值:
print(myString!)
But if you want to unwrap automatically:
但是如果你想自动打开包装:
var myString: Int! = 55
Then:
然后:
print(myString)
No need to unwrap. Hope it will help.
无需打开包装。希望它会有所帮助。