What is difference between
self?.profile!.id!
and
(self?.profile!.id!)!
XCode converts first to second.
The first one contains self?
which means self
is optional, leads to let related properties (profile!.id!
in your case) related to the existence of the self
which is Optional Chaining:
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.
To make it more simpler, you could think of id!
nullity is also optional, even if you force unwrapping it, because it is related to the existence of self
; If self
is nil
, profile
and id
will be also nil
implicitly because they are related to the existence of self
.
Mentioning: (self?.profile!.id!)!
means that the whole value of the chain would be force wrapped.
Note that implementing:
self!.profile!.id!
leads to the same output of
(self?.profile!.id!)!
since self!
is force unwrapped, the value of id
would not be related to the nullity of self
because the compiler assumes that self
will always has a value.
However, this approach is unsafe, you should go with optional binding.