Q re creating selectedIndexPath var My Locations pg107

In creating the category picker, you instantiate the

var selectedIndexPath = IndexPath()

but as far as I can see never use that initial object, just to replace it with a new one during viewDidfLoad.

Wouldn’t it be better to either create it during the segue or, easier, just have it as an option and create it when it’s needed?

It seems a waste of processing to create it and immediately throw it away?

cheers
Darren

Good question. :smiley:

This is a little trick to avoid using optionals.

If you only want to fill in this variable at a later point, selectedIndexPath must be an optional since all instance variables need to have a value when the class is created. I prefer to use optionals as little as possible.

Is it a waste of processing? Not really. It does do a little work that is unused and that could be avoided by using an optional. But an optional adds a little work every time you want to use it, so in the end the amount of processing time used comes to about the same thing.

This kind of performance optimization is not worth worrying about, to be honest. This is also referred to as “premature optimization”. :wink:

OK, thanks. That’s what I assumed, but wanted to check I wasn’t missing anything.

In these days of recycling it just felt a bit wrong to create an object just to throw it away without using it :slight_smile:

Thanks.

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