# Q re creating selectedIndexPath var My Locations pg107

**URL:** <https://forums.kodeco.com/t/q-re-creating-selectedindexpath-var-my-locations-pg107/34194>\
**Category:** iOS Apprentice\
**Created:** [September 8, 2017, 12:02pm UTC](https://forums.kodeco.com/t/q-re-creating-selectedindexpath-var-my-locations-pg107/34194 "2017-09-08T12:02:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flanker](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/flanker/32/109129_2.png) [@flanker](https://forums.kodeco.com/u/flanker)\
**Post date:** [September 8, 2017, 12:02pm UTC](https://forums.kodeco.com/t/q-re-creating-selectedindexpath-var-my-locations-pg107/34194/1 "2017-09-08T12:02:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hollance](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/hollance/32/109009_2.png) [@hollance](https://forums.kodeco.com/u/hollance)\
**Post date:** [September 9, 2017, 9:04am UTC](https://forums.kodeco.com/t/q-re-creating-selectedindexpath-var-my-locations-pg107/34194/2 "2017-09-09T09:04:08Z")

</div>

Good question. 😃

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”. 😉

---

<div class="post-metadata">

**Author:** ![flanker](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/flanker/32/109129_2.png) [@flanker](https://forums.kodeco.com/u/flanker)\
**Post date:** [September 11, 2017, 9:06am UTC](https://forums.kodeco.com/t/q-re-creating-selectedindexpath-var-my-locations-pg107/34194/3 "2017-09-11T09:06:38Z")

</div>

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 🙂

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [February 22, 2018, 4:02am UTC](https://forums.kodeco.com/t/q-re-creating-selectedindexpath-var-my-locations-pg107/34194/4 "2018-02-22T04:02:28Z")

</div>

Closed on February 22, 2018, 4:02am UTC

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