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.
On
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 meansselfis optional, leads to let related properties (profile!.id!in your case) related to the existence of theselfwhich 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; Ifselfisnil,profileandidwill be alsonilimplicitly 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 ofidwould not be related to the nullity ofselfbecause the compiler assumes thatselfwill always has a value.However, this approach is unsafe, you should go with optional binding.