Checklist !unwrapped optionals explanation - pg 428

Hi there, the two lines of code I’m curious about on pg 428 are:

cell.textLabel!.text = someString
cell.detailTextLabel!.text = anotherString

I understand the reason we force unwrap textLabel and detailTextLabel, however, I was hoping someone could shed some light as to why we do not need to unwrap text as well? When I inspect it, it says it returns an optional String.

So why do we not need something like:

cell.textLabel!.text! = someString
cell.detailTextLabel!.text! = anotherString

The short answer: you are assigning something to the text property, so you don’t care if it is currently nil or not.

The reason you need it on textLabel is to get to the text property in the first place. If textLabel was nil there would be no text property to assign to.

Short and sweet. Thanks once again @sgerrard for yet another succinct and clear reply!

This topic was automatically closed after 166 days. New replies are no longer allowed.