Sergio Bost
Sergio Bost

Reputation: 3219

Iterating over an optional using enumerated

Having problems iterating through an optional enumerated type. Looking for general guidance if anyone has any and will post any code if needed.

It all stems from this

@Published var movielist: Movie?

but I'm really confused on how to handle this error.

enter image description here

Upvotes: 2

Views: 49

Answers (1)

jnpdx
jnpdx

Reputation: 52525

(vm.movielist?.results ?? []).enumerated()

This way, if vm.movielist is nil, then an empty collection gets used instead.

(this is the first "Fix" solution that Xcode gives, although sometimes it gets the syntax a little bit funny when it tries to fix things automatically that are in a chain like this)

Upvotes: 3

Related Questions