What is difference between
self?.profile!.id!
and
(self?.profile!.id!)!
XCode converts first to second.
What is difference between
self?.profile!.id!
and
(self?.profile!.id!)!
XCode converts first to second.
First of all you are using too many question and exclamation marks!!!
Practically there is no difference. The result is a forced-unwrapped optional.
Usually Xcode suggests that syntax if the result of the last item of the chaining is a non-optional so the exclamation mark would cause an error for example
text?.count!
Then Xcode suggests
(text?.count)!
but in this case be brave and write
text!.count
The first one contains
self?
which meansself
is optional, leads to let related properties (profile!.id!
in your case) related to the existence of theself
which is Optional Chaining: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 ofself
; Ifself
isnil
,profile
andid
will be alsonil
implicitly because they are related to the existence ofself
.Mentioning:
(self?.profile!.id!)!
means that the whole value of the chain would be force wrapped.Note that implementing:
leads to the same output of
since
self!
is force unwrapped, the value ofid
would not be related to the nullity ofself
because the compiler assumes thatself
will always has a value.However, this approach is unsafe, you should go with optional binding.