Beginning iOS Animations · Introduction | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/9051-beginning-ios-animations/lessons/21

@catie, @jessycatterwaul, what is the type of var herb: HerbModel! in HerbDetailsViewController? And why do we use this type instead ordinary HerbModel ?

Hi! HerbModel! is an implicitly unwrapped optional HerbModel. Basically, it’s an optional and we’re saying “I promise this will have a value when it’s accessed, so just unwrap it for me!”.

Practically, this lets us keep the code complexity down in the starter sample project.

But where do we declare HerbModel is of optional type? :thinking:

The exclamation mark right after HerbModel is marking it as an optional:

var herb: HerbModel👉!👈

And what is the difference between ? and ! then? :confused:

It’s possible this isn’t explicitly discussed in our Swift courses right now, but it’s a similar concept to force unwrapping.

Both ? and ! can be used to mark a type as optional.

HerbModel? gives you an optional HerbModel. You have to manually unwrap it yourself when you try to access them later.

HerbModel! gives you an implicitly unwrapped optional HerbModel. This is automatically unwrapped for you when you try to access it. You’ll see these kind of optionals when working with IBOutlets. You can still use safe unwrapping techniques on implicitly unwrapped optionals, you just don’t have to.

1 Like

Wow! Thank you for the explanation!=)

1 Like