Something About Optional Assignment
We all know that in Swift
, optional chaining
is an important feature that can help us write concise and elegant code.
However, the optional assignment
derived from the optional chain may have some strange phenomena when used.
The following discussion and code are based on
Swift5.7
We often use optional chaining for value or assignment in code, such as
1 | class Person { |
In the Swift documentation it is described as follows
Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil.
If the optional contains a value, the property, method, or subscript call succeeds;
if the optional is nil, the property, method, or subscript call returns nil.
Multiple queries can be chained together, and the entire chain fails gracefully if any link in the chain is nil.
Optional chaining can actually be used in assignment
operations, such as
1 | var someThing: String? |
If it is nil, the assignment operation will not be performed, and nil will be returned.
In some cases, it may play a role.
But when there are multiple optional
types, things get a little more complicated.
Multiple optional types are not as rare as you might think, such as
Sometimes when you want to leave some placeholder keys in the dictionary, you may write like this.
Of course, I personally don’t recommend such a design type
1 | var sth: [String: String?] = ["x": "y"] |
At this point, optional assignment may have some ambiguous things happen
1 | sth["x"] = nil |
So what actually happens is something like
1 | if sth["x"] != nil { |
We will find a similar phenomenon when we reduce directly to double optional types
1 | var tt: String?? = nil |
To sum up, optional assignment
is an operation that may be useful in some scenarios, but be sure to evaluate the possible risks when using it.
Multiple optional
types may bring unexpected performance, which is related to the underlying implementation of Swift’s implicit wrapping, unwrapping, etc., and needs to be used with caution.